2 回答

TA貢獻1789條經(jīng)驗 獲得超10個贊
問題在于,所有包含學(xué)生ID的隱藏字段都放在一種表單中。因此,ID當(dāng)您單擊任何刪除按鈕時,都會發(fā)布最后一個隱藏字段。將form標(biāo)簽分別放置Delete在每一行的列內(nèi),然后僅發(fā)布單擊的行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>
您還可以創(chuàng)建刪除鏈接(即test.php的?delete_id = 100)為每一行單獨而不是創(chuàng)建的form和GETID刪除服務(wù)器端。

TA貢獻1836條經(jīng)驗 獲得超4個贊
您需要更改查詢:
$query="Delete From student where id=$did";
反而
$query="Delete From student where id='$did'";
- 2 回答
- 0 關(guān)注
- 409 瀏覽
添加回答
舉報