需要帮助以post形式递增2个变量

时间:2011-04-02 14:58:30

标签: php mysql forms input

我正在尝试允许用户使用POST从显示的表单中编辑一行数据。查询正在运行,表格正确显示表单输入字段中除名称值之外的所有内容。我尝试了很多变化,但名称值一直空白。问题可能在于这一行:

       echo $field_name;

以下是代码:

<form action="process.php" method="POST">

<?

$qry = "SELECT activity, site, date, FROM home WHERE user_id='$session->user_id' ORDER BY date";
$res = mysql_query($qry);
$field_name = mysql_field_name($res, 0);

function mysql_fetch_all($res) {

    while($row=mysql_fetch_array($res)) {
        $return[] = $row;
    }

   return $return;
}

function create_table($dataArr) {
    echo "<form action=\"process.php\" method=\"POST\"><table><tr>";
    for($j = 0; $j < 3; $j++) {
        echo "<td><input type=\"text\" name=\"";
        echo $field_name;
        echo "\" maxlength=\"30\" value=" .$dataArr[$j]. "></td>";
    }
    echo "<td><input type=\"hidden\" name=\"subedit\" value=\"1\"><input type=\"submit\" value=\"Update\"></td></tr></table></form>";
}

$all = mysql_fetch_all($res);

echo "<table class='data_table'>";
echo "<tr><td colspan=\"3\"><h2>Current Profile</h2></td></tr>";
echo "<tr><small><td>Activity&nbsp;</td><td>Site&nbsp;</td><td>Date&nbsp;</td></small></tr>";

for($i = 0; $i < count($all); $i++) {
    create_table($all[$i]);
}

echo "</table></form>";

2 个答案:

答案 0 :(得分:0)

$field_name超出范围(请查看PHP's variable scope页面)。尝试更改create_table功能以接受$field_name var,如下所示:

function create_table($dataArr, $field_name) {
...
}
...
for($i = 0; $i < count($all); $i++) {
    create_table($all[$i], $field_name);
}

使用全球(不推荐)

function create_table($dataArr) {
    global $field_name;
    ...
}

答案 1 :(得分:0)

试试这段代码:

<form action="process.php" method="POST">

<?

$qry = "SELECT activity, site, date, FROM home WHERE user_id='$session->user_id' ORDER BY date";
$res = mysql_query($qry);
$field_name = mysql_field_name($res, 0);

function mysql_fetch_all($res) {

    while($row=mysql_fetch_array($res)) {
        $return[] = $row;
    }

   return $return;
}

function create_table($dataArr, $field_name) {
    echo "<form action=\"process.php\" method=\"POST\"><table><tr>";
    for($j = 0; $j < 3; $j++) {
        echo "<td><input type=\"text\" name=\"";
        echo $field_name;
        echo "\" maxlength=\"30\" value=" .$dataArr[$j]. "></td>";
    }
    echo "<td><input type=\"hidden\" name=\"subedit\" value=\"1\"><input type=\"submit\" value=\"Update\"></td></tr></table></form>";
}

$all = mysql_fetch_all($res);

echo "<table class='data_table'>";
echo "<tr><td colspan=\"3\"><h2>Current Profile</h2></td></tr>";
echo "<tr><small><td>Activity&nbsp;</td><td>Site&nbsp;</td><td>Date&nbsp;</td></small></tr>";

for($i = 0; $i < count($all); $i++) {
    create_table($all[$i], $field_name);
}

echo "</table></form>";

但是,我建议您尝试创建Table类。它会更有效率和更整洁。