2 回答
TA贡献1789条经验 获得超10个赞
问题在于,所有包含学生ID的隐藏字段都放在一种表单中。因此,ID当您单击任何删除按钮时,都会发布最后一个隐藏字段。将form标签分别放置Delete在每一行的列内,然后仅发布单击的行ID。还要在SELECT查询后放置查询,DELETE以在删除后立即刷新HTML表。您还需要避免SQL注入。
<?php
$link = mysqli_connect( "localhost", "root", "" ) or die( mysqli_error() );
mysqli_select_db( $link, "myDataBase" ) or die( mysqli_error() );
// delete record
if( isset( $_POST['delete'] ) ) {
echo $did = $_POST['id'];
$query = $link->prepare( "DELETE FROM student WHERE id=?" );
$query->bind_param( "s", $did );
$query->execute();
}
// get all records
$query = "SELECT * FROM student" or die( mysqli_error() );
$result = mysqli_query( $link, $query );
?>
<table border="1">
<tr>
<th>Student Name</th>
<th>Matric Number</th>
<th>IC Number</th>
<th>Update</th>
<th>Delete</th>
</tr>
<?php
if( $result->num_rows > 0 ) {
while( $row = $result->fetch_assoc() ) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>" . $row["matric"] . "</td>";
echo "<td>" . $row["ic"] . "</td>";
echo "<td><input type=button value=Update></td>";
echo "<td><form method='POST'>
<input type=hidden name=id value=".$row["id"]." >
<input type=submit value=Delete name=delete >
</form>
</td>";
echo "</tr>";
}
} else {
die("0 results");
}
?>
</table>
您还可以创建删除链接(即test.php的?delete_id = 100)为每一行单独而不是创建的form和GETID删除服务器端。
TA贡献1836条经验 获得超4个赞
您需要更改查询:
$query="Delete From student where id=$did";
反而
$query="Delete From student where id='$did'";
- 2 回答
- 0 关注
- 387 浏览
添加回答
举报