gpt4 book ai didi

PHP post 总是返回最后一个值

转载 作者:行者123 更新时间:2023-11-30 23:58:07 26 4
gpt4 key购买 nike

我正在尝试创建一个简单的 PHP 页面来检索和显示来自数据库的信息,所以我这样做了,效果很好:

echo "<form action='#' method='post'>";
if(!empty($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
echo "<td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
echo "\t</tr>\n";
}
}
echo "</form>";

每一行都有一个删除按钮,这将允许用户单独删除每一行,所以我查看是否已单击删除按钮,然后通过获取 ID 继续删除该行:

if(isset($_POST['deleteItem'])){
$querydelete = "DELETE FROM address WHERE ID = " . $_POST["value_id"];
mysql_query($querydelete) or die('Query failed: ' . mysql_error());
}

问题是发帖后ID值总是一样的(也就是最后一行的ID值)。如果我预先显示每一行的 ID,它就是正确的,所以问题一定是它在帖子中获得的值(value)。我做错了什么?

最佳答案

这是因为您在表单中创建了许多具有相同名称的字段。您可以为每条记录创建单独的表单:

if(!empty($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<form action='#' method='post'>"
echo "\t<tr>\n";
echo "<td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
echo "\t</tr>\n";
echo "</form>";
}
}

关于PHP post 总是返回最后一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24368869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com