我试图让我的PHP脚本从表中删除特定的行。每行都有一个唯一的ID。但是,我的代码总是删除第一行,而例如我希望它删除我单击按钮的行。
$task_id = $row_list['task_id'];
if (!empty($_GET['delete'])) {
mysql_query("DELETE FROM to_do.list WHERE list.task_id=$task_id");
header('Location: http://localhost/to_do/list_view.php');
};
表格+脚本触发器:
do { ?>
<?php echo "<table><tr>";
echo "<td>" . $row_list['task_id'] . "</td>";
echo "<td>" . $row_list['task_info'] . "</td>";
echo "<td>" . "<form action=script.php method=get name=delete>
<input type=hidden name=delete value=$task_id>
<input type=submit value=Delete></form>" . "</td>"; ?>
<?php } while ($row_list = mysql_fetch_assoc($list));
echo "</table>" ?>
我不确定如何让它发挥作用。提前谢谢!
答案 0 :(得分:-2)
也许
if (isset($_GET['submit'])) {
mysql_query("DELETE FROM to_do.list WHERE list.task_id = $_GET['delete']");
header('Location: http://localhost/to_do/list_view.php');
};
和
echo "<td>" . "<form action=script.php method=get>
<input type=hidden name=delete value=$task_id>
<input type=submit name=submit value=Delete></form>" . "</td>"; ?>