yellowzm Posted February 17, 2009 Share Posted February 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145631-solved-deleting-from-db-strange-happenings-please-assist/ Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 Thats because you have to use teh GET var DELETE FROM events WHERE id=$_GET[id] Quote Link to comment https://forums.phpfreaks.com/topic/145631-solved-deleting-from-db-strange-happenings-please-assist/#findComment-764538 Share on other sites More sharing options...
yellowzm Posted February 17, 2009 Author Share Posted February 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145631-solved-deleting-from-db-strange-happenings-please-assist/#findComment-764544 Share on other sites More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/145631-solved-deleting-from-db-strange-happenings-please-assist/#findComment-764583 Share on other sites More sharing options...
yellowzm Posted February 18, 2009 Author Share Posted February 18, 2009 Thank you very much for the clear explanation. I appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/145631-solved-deleting-from-db-strange-happenings-please-assist/#findComment-764709 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.