Question

This Table shows all the 'Attractions' which a user has added on my website - a library where they can keep note. They can delete any of these 'Attractions' if they wish by clicking on the 'empty_trash-26.png' image. Which links to drop_attraction.php

My problem is when a user goes to delete one 'Attraction' they have already added, it deletes the latest 'Attraction' added to the website/database. I presume that in the while loop the $_SESSION['attraction_id'] is overwritten every time by the newest entity.

Does anyone know how I can prevent this? - so they can delete whatever one they want and not just the newest!!

// 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>"; 

Below is the drop_attraction.php page - once the 'empty_trash-26.png' gets clicked, the php page is redirect to this page so the row can be deleted in the 'Attractions' table in my database. Once that is done then its linked straight back to the 'dashboard-library.php' page (code shown above)

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

Solution

Your page with the attractions table should pass the id to the drop attractions page:

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');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top