Question

I have 2 web pages which amend a news blog, and if I want to delete a blog it opens up delete.php.

I retrieve the blogs using

$query = $db->prepare('SELECT * FROM `news` order by date DESC'); 

$query->execute(); 
$query->store_result(); 
$query->bind_result($id,$title); 

while ($query->fetch()) {


$_SESSION['id'] = $id ;
$_SESSION['title'] = $title ;
<a href="deletenews.php">
}

I make 2 sessions for the $id and $title. I have a delete button, that goes to delete.php,

$id = $_SESSION['id'];
$title = $_SESSION['title'];



 $query = $db->prepare('DELETE  FROM `news` WHERE id = ? and title = ?'); 
 $query->bind_param('ss', $id, $title);
 $query->execute(); 

unset($_SESSION['id']);
unset($_SESSION['title']);
header("location: news.php");

I call the $id and $title session to test, on amend page, which is working fine, but when it goes over to delete.php, it changes. It is deleting from the database, but instead of e.g. id 1 it will delete id 2, it will delete the next one up the list in the database. I find it very strange.

If anyone could help...

Was it helpful?

Solution

If you have 2 records the first one id = 1, second id = 2

while ($query->fetch()) {


   $_SESSION['id'] = $id ;
   //First loop $_SESSION['id'] = 1
   //second loop $_SESSION['id'] = 2
   $_SESSION['title'] = $title ;
   <a href="deletenews.php">
}
// at the end $_SESSION['id'] = 2 

What you want to do is

while ($query->fetch()) {

   <a href="deletenews.php?id=$id&title=$title">
   //it will link the link ;) with the id of the news
}

The changes in delete.php will be :

$id = $_GET['id'];
$title = $_GET['title'];

have fun

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top