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.

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.