這個問題非常基本,我在stackoverflow中發現了很多類似的問題,但是他們沒有一個適合我。使用php刪除表格的特定行使用php
我已經設計了將顯示數據是這樣的表:
ID name Delete
1 abc Delete
2 def Delete
用於上述顯示的代碼是
<?php
$con=mysqli_connect("abc","abc","abc","abc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM student");
echo "<table class='table table-striped table-bordered table-hover'>
<thead>
<tr>
<th>ID</th>
<th>name</th>
<th>delete</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody data-link='row' class='rowlink'>";
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><a href='delete.php'>Delete</a></td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
mysqli_close($con);
?>
爲delete.php代碼
<?php
$con=mysqli_connect("abc","abc","abc","abc");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM student WHERE id='$id'");
mysqli_close($con);
header("Location: index.php");
?>
查看數據庫是
Id name
1 abc
2 cdf
的問題是,它不刪除數據,也沒有表現出任何差錯
我是新來這個領域,將不勝感激,如果有人可以幫助我
您尚未在您的刪除代碼中定義'$ id'; *或有你?*沒有顯示/包含。 – 2014-09-29 19:47:00
您也將引號放在整數週圍,並且無法檢查函數返回值。 – Sammitch 2014-09-29 19:49:17
讓你的刪除腳本成爲一個簡單的GET腳本,肯定會把你踢到臀部,並踩到你的肋骨。切勿將這樣的破壞性操作放在get後面 - 請考慮如果瀏覽器決定預取頁面上的所有鏈接會發生什麼情況。 「哎呀呀」。 – 2014-09-29 19:49:32