Jump to content

[SOLVED] Deleting from DB strange happenings please assist


yellowzm

Recommended Posts

I am trying to create a way to delete old information from the db for my kids wrestling teams website that I got asked to create. so far I have managed to get everything working(some help from ppl here was needed). The code below is what I was trying to use and it doesn't quite do what I think it should. Instead of deleting the row with the selected id like it should it deletes the row that is displayed at the bottom of the page. I have change the ORDER BY from DESC to ASC and it still deletes the post listed at the bottom of the page regardless of the id.

 

<? 
mysql_connect("localhost","user","pass");
mysql_select_db("teambrav_teambraves"); 
if(!isset($cmd)) 
{
   $result = mysql_query("select * from events"); 
   while($r=mysql_fetch_array($result)) 
   { 
      $title=$r["eventtitle"];
      $id=$r["id"];
      echo "<a href='deletetest.php?cmd=delete&id=$id'>$title - Delete</a>";
      echo "<br>";
    }
}
if($_GET["cmd"]=="delete")
{
    $sql = ("DELETE FROM events WHERE id=$id")or die(mysql_error()); ;
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>

 

I am by no means dead set on using this code so If someone has a better way to do this I am open to the suggestion. but I would like to know why this is doing what it is doing.

 

Thanks in advance for any help you can give.

 

P.S. I am still new to PHP and MySQL so take it easy on me.

this appears to make it work properly, thanks for the help, I would still like to know why it was doing what it was. I am going to test this with more than 2 posts real quick and I'll come mark as resolved if no more problems occur.

You have a code that selects prints all items from 'events' table. After it's done, the $id is equal to the id of last item displayed. THen the part of the code responsible for deleting begins... and it uses this $id to delete... yup, the last item.

 

When you change the code, to use $_GET[$id], you get the id of the item to be deleted from url

deletetest.php?cmd=delete&id=$id

 

($_GET is an array that stores all variables passed through url)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.