2017-04-19 70 views
0

這是我delete.php頁:無法獲得排刪除PHP

<?php 
//including the database connection file 
include("config.php"); 

//getting id of the data from url 
$id = $_GET['id']; 

//deleting the row from table 
$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id"); 

//redirecting to the display page (index.php in our case) 
header("Location:index.php"); 
?> 

和被調用命中index.php頁面有:

<?php 
//including the database connection file 
include("config.php"); 

$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id ASC"); 
// using mysqli_query instead 


while($res = mysqli_fetch_array($result)) 
    { 
?> 
<table> 
<tr> 
    <td class="long"><?php echo $res['nameFirst'];?></td> 
    <td class="long"><?php echo $res['nameLast'];?></td> 
    <td class="long"><?php echo $res['email'];?></td> 
    <td><a href="edit.php?id=$res[id]">Edit</a></td> 
    <td><a href="delete.php?id=$res[id]" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td> 
</tr> 
<?php } ?> 

</table> 

但是當我點擊刪除鏈接,頁面要求確認,然後刷新本身,因爲它重新加載index.php(本身),但表中的行不會刪除。

我相信這是一個簡單的修復,但看不到問題。我假設錯誤在$ result行中的某處?

預先感謝您。

+0

'$水庫[ID]'必須保持PHP標籤內 – Saty

+0

** WARNING **:當使用'mysqli'你應該使用[參數化查詢](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param'](http://php.net/manual/en/mysqli- stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 **不要**將'$ _POST','$ _GET'或**任何**用戶數據直接放入查詢中,如果有人試圖利用您的錯誤,這可能會非常有害。 – tadman

+0

需要注意的是,通過GET風格的鏈接來刪除東西通常是一個超好的主意。某些瀏覽器和網頁抓取工具可以觸發這些鏈接,最終可能會系統地刪除數據庫中的所有內容。出於這個原因,大多數人至少堅持把它們放在POST後面。 – tadman

回答

0

它,因爲你是用HTML和PHP代碼搞亂。

並且PHP代碼不生成id。

我們可以在任何地方用PHP文件交替編寫PHP,HTML和Javascript代碼。

但是,您需要提及哪些是PHP代碼,哪些是HTML代碼(如果沒有指定,則爲默認值),哪些是Javascript代碼。

修改行:

<td><a href="delete.php?id=$res[id]" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td> 

要:

<td><a href="delete.php?id=<?php echo $res['id'];?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td> 
0

當您在inxde.php中傳遞ID參數時,您沒有顯示ID。看到下面的正確的代碼:

<?php 
//including the database connection file 
include("config.php"); 

$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id ASC"); 
// using mysqli_query instead 


while($res = mysqli_fetch_array($result)) 
    { 
?> 
<table> 
<tr> 
    <td class="long"><?php echo $res['nameFirst'];?></td> 
    <td class="long"><?php echo $res['nameLast'];?></td> 
    <td class="long"><?php echo $res['email'];?></td> 
    <td><a href="edit.php?id=$res[id]">Edit</a></td> 
    <td><a href="delete.php?id=<?php echo $res['id']; ?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td> 
</tr> 
<?php } ?> 

</table> 
0

修正這些行:

<td><a href="edit.php?id=$res['id']">Edit</a></td> 
<td><a href="delete.php?id=$res['id']" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td> 

$res[id]需求是$res['id'],因爲你傳遞列名作爲字符串。

+0

謝謝你的幫助。 – user3858836

0

你忘了呼應

<a href="delete.php?id=<?php echo $res[id] ?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a> 
+0

謝謝,非常感謝你。 – user3858836

1

試試這個

<td><a href="edit.php?id="<?php echo $res['id']; ?>>Edit</a></td> 
<td><a href="delete.php?id="<?php echo $res[id]; ?> onClick="return confirm('Are you sure you want to delete?')">Delete</a></td> 

delete.php

$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=".$id);