我在尝试删除特定记录时从数据库表中获取数据无论我点击最后一条记录都会删除我做错了什么,这是在提交/点击del按钮后使用的代码
// DELETE
if(isset($_POST['del']))
{
require'conn.php';
$delete_id = $_POST['del_id'];
print_r($_POST);
die;
$del_stmt = "DELETE FROM signup WHERE ID =$delete_id";
mysqli_query($conn,$del_stmt);
mysqli_execute($del_stmt);
$row=mysqli_affected_rows($conn);
if($row==1)
{
echo "<h1>".' sucess ! record was deleted' ."</h1>";
}
else
{
echo "<h1>".' record was not deleted '."</h1>";
}
mysqli_close($conn);
}
include'fetchtable.php';
这是我的表结构和del按钮代码
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
}
?>
</form>
答案 0 :(得分:0)
每个操作都需要多个,因此您需要将代码设为
while (mysqli_stmt_fetch($stmt)){
echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">' ;
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo "</tr>";
echo "</form>";
}
此外,您的代码不安全,您需要将mysqli_real_escape_string()用于POST数据。或准备好的声明
答案 1 :(得分:0)
正如Abhik Chakraborty所说,你需要每一行的表格或其他逻辑;
一种解决方案是将<form ...>...</form>
置于循环中:
这是 - AFAIK - 不正确,in不符合HTML标准。并且仅适用于某些浏览器,因为<table>
和<form>
混合的顺序错误,我只将其用作示例来显示问题
<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo "<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">"
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
echo "</form>"
我希望表单中只有一个隐藏字段,并使用提交按钮的onClick
事件设置该字段的值。
只有最后一行:
...
echo '<td> <input type="submit" name="del" value="delete" onclick="form.row_id.value='$the id$';"/>'
echo '<td> <input type="submit" name="edit" value="edit" onclick="form.row_id.value='$the id$';"/>'
echo"</tr>";
}
?>
<input ID='row_id' type="hidden" name="del_id" value="no set till now" />'
</form>