2014-02-21 44 views
2

此表顯示了用戶在我的網站上添加的所有「景點」 - 一個可以記錄的庫。如果希望點擊'empty_trash-26.png'圖片,他們可以刪除這些'景點'。哪些鏈接drop_attraction.php會話ID在while循環中被覆蓋。如何預防它?

我的問題是,當用戶去刪除他們已經添加的'吸引',它刪除了最新的'吸引'添加到網站/數據庫。我假設在while循環中,每次由最新的實體覆蓋$ _SESSION ['attraction_id']。

有誰知道我可以如何防止這種情況? - 所以他們可以刪除他們想要的任何一個,而不僅僅是最新的!

// Connects to my Database 
include "Config.php"; 
$dataA = mysql_query("SELECT * FROM Attractions WHERE user_id = ".$_SESSION['id']."") or die(mysql_error()); 

// HEADER OF TABLE    
Print "<table id="."box-table-a"." summary="."Deals".">"; 
Print "<thead>"; 
Print "<tr>"; 
Print "<th scope="."col".">Categories</th>"; 
Print "<th scope="."col".">Item Name</th>"; 
     Print "<th scope="."col".">Price</th>"; 
     Print "<th scope="."col"."> </th>"; 
    Print "</tr>"; 
Print "</thead>"; 

// ATTRACTIONS 
while($infoA = mysql_fetch_array($dataA)) 
{ 

Print "<tbody>"; 
    Print "<tr>"; 
    Print "<td>"."Attractions"."</td>"; 
     Print "<td>".$infoA['attraction_name'] . "</td>"; 
     Print "<td>".$infoA['attraction_price'] . " </td>"; 

$_SESSION['attraction_id'] = $infoA['id']; 
$_SESSION['attraction_name'] = $infoA['attraction_name']; 
$_SESSION['attraction_price'] = $infoA['attraction_price']; 


     Print "<td><a href="."drop_attraction.php"."> <img src="."images/empty_trash-26.png"." width="."20"." height="."20"."></a></td>"; 
    Print "</tr>"; 
Print "</tbody>"; 
} 
Print "</table>"; 

下面是drop_attraction.php頁面 - 一旦「empty_trash-26.png」被點擊,PHP頁面重定向是這個頁面,以便該行可以在「景點」表在我的數據庫中刪除。一旦做到這一點,然後它的鏈接直接回到「儀表板library.php頁面(如上圖所示的代碼)

include "config.php"; 
mysql_query("DELETE FROM Attractions WHERE id=".$_SESSION['attraction_id'].""); 
header('Location: /dashboard-library.php'); 
+0

避免'mysql_ *'功能,反正。 –

+0

你有'$ _SESSION ['attraction_id']'和'id =「。$ _ SESSION ['atrraction_id']。」'...錯字?在'Attraction'表中,這個屬性被稱爲'id',我將它存儲在$ _SESSION ['attraction_id']中。'__ SESSION ['attraction_id']。'' –

+0

@ ]。不明白你的意思?對不起 – user3339063

回答

0

您的網頁與景點表應該ID傳遞到刪除景點頁:

Print "<td><a href="."drop_attraction.php?id=".$infoA['id']."> <img src="."images/empty_trash-26.png"." width="."20"." height="."20"."></a></td>"; 

drop_attractions.php

include "config.php"; 
mysql_query("DELETE FROM Attractions WHERE id=".mysql_real_escape_string($_GET['id'])); 
header('Location: /dashboard-library.php'); 
+2

小心添加一條關於爲什麼發生在他身上的筆記? – bobkingof12vs

+0

非常感謝。欣賞它! – user3339063