2014-09-29 81 views
3

這個問題非常基本,我在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 

的問題是,它不刪除數據,也沒有表現出任何差錯

我是新來這個領域,將不勝感激,如果有人可以幫助我

+0

您尚未在您的刪除代碼中定義'$ id'; *或有你?*沒有顯示/包含。 – 2014-09-29 19:47:00

+0

您也將引號放在整數週圍,並且無法檢查函數返回值。 – Sammitch 2014-09-29 19:49:17

+1

讓你的刪除腳本成爲一個簡單的GET腳本,肯定會把你踢到臀部,並踩到你的肋骨。切勿將這樣的破壞性操作放在get後面 - 請考慮如果瀏覽器決定預取頁面上的所有鏈接會發生什麼情況。 「哎呀呀」。 – 2014-09-29 19:49:32

回答

5

改變這一行:

echo "<td><a href='delete.php'>Delete</a></td>"; 

echo "<td><a href=\"delete.php?id=".$row['id']."\">Delete</a></td>"; 

那麼對於delete.php(和原先的評論稱,$id沒有定義)。

<?php 
$con=mysqli_connect("abc","abc","abc","abc"); 
// Check connection 
if (mysqli_connect_errno()) { 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$id = $_GET['id']; // $id is now defined 

// or assuming your column is indeed an int 
// $id = (int)$_GET['id']; 

mysqli_query($con,"DELETE FROM student WHERE id='".$id."'"); 
mysqli_close($con); 
header("Location: index.php"); 
?> 

,它會工作。

然而,爲了安全起見,你應該考慮使用mysqli with prepared statements,或PDO with prepared statements他們更安全。我在下面列出了一個例子。


這裏是一個準備好的語句例如:

<?php 
$DB_HOST = "xxx"; 
$DB_NAME = "xxx"; 
$DB_USER = "xxx"; 
$DB_PASS = "xxx"; 

$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); 
if($con->connect_errno > 0) { 
    die('Connection failed [' . $con->connect_error . ']'); 
} 

$id = (int)$_GET['id']; 

$update = $con->prepare("DELETE FROM student WHERE id = ?"); 
$update->bind_param('i', $id); 
$update->execute(); 
$update->close(); 
+1

非常感謝你,它非常完美 – Sam 2014-09-30 03:59:44

+0

@Sam你非常歡迎山姆。 – 2014-09-30 04:00:33

0

一個GET參數添加到您的鏈接:

echo "<td><a href='delete.php?id='".$row['id']."'>Delete</a></td>"; 

然後抓住它在你的delete.php查詢:

$id = $_GET['id']; 
mysqli_query($con,"DELETE FROM student WHERE id='".$id."'"); 
+1

修改數據庫中的數據時,使用'GET'永遠不是一個好主意。使用例如預取瀏覽器擴展,您將自動清除數據庫。 – jeroen 2014-09-29 19:50:29

+0

我試過了,但沒有返回任何結果 – Sam 2014-09-29 19:51:51

+0

爲什麼不是個好主意? 你可以通過intval($ id)清理$ id,然後它是安全的。 – Carca 2014-09-29 19:59:54