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

发布于 2024-10-28 23:27:14 字数 1497 浏览 2 评论 0原文

我试图允许用户从显示的表单中使用 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>";

I'm trying to allow a user to edit a row of their data with POST from a displayed form. The query is working and the table properly displays everything except a name value in the form input field. I've tried numerous variations but the name value keeps coming up blank. The problem might be with this line:

       echo $field_name;

Here is the code:

<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 </td><td>Site </td><td>Date </td></small></tr>";

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青芜 2024-11-04 23:27:14

$field_name 超出范围(查看 PHP 的变量范围 页面)。尝试更改您的 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;
    ...
}

$field_name is out of scope (check out PHP's variable scope page). Try changing your create_table function to accept the $field_name var like so:

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

Or using global (not as recommended)

function create_table($dataArr) {
    global $field_name;
    ...
}
享受孤独 2024-11-04 23:27:14

尝试以下代码:

<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 </td><td>Site </td><td>Date </td></small></tr>";

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

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

但是,我确实建议您尝试创建一个 Table 类。这会更加高效和整洁。

Try this code:

<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 </td><td>Site </td><td>Date </td></small></tr>";

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

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

However, I do recommend that you try to create a Table class. It would be much more efficient and neater.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文