wartotojas Posted November 27, 2009 Share Posted November 27, 2009 Hey there, I have a very basic php code to delete single row in a table, the problem is that it doesn't work here it is: <html> <body> <? //connect to mysql mysql_connect("localhost","username","password"); //select which database you want to edit mysql_select_db("db_namer"); //If cmd is not hit if(!isset($cmd)) { //display all the files $result = mysql_query("select * from tbl_name"); //run the while loop that grabs all the names in table while($r=mysql_fetch_array($result)) { //grab the title and the ID $title=$r["name"]; //take out the title $id=$r["id"]; //take out the id echo "<a href='delete.php?cmd=delete&id=$id'>$title - Delete</a>"; echo "<br>"; } } ?> <? if($cmd=="delete") { $sql = "DELETE FROM tbl_name WHERE id = $id"; $result = mysql_query($sql); echo "Row deleted!"; } ?> </body> </html> it returns all records that are in table, but when I hit delete - nothing. It is probably something simple, but I just cant figure it out by my self. Thanx Quote Link to comment https://forums.phpfreaks.com/topic/183116-phpmysql-delete-row-script/ Share on other sites More sharing options...
Anzeo Posted November 27, 2009 Share Posted November 27, 2009 Is $cmd set? can you give me the result you get when you add var_dump($cmd) to your code? Also preferably use the long opening tag: <?php i.s.o <? Finally why do you close and reopen the php tags in the middle of your script? Quote Link to comment https://forums.phpfreaks.com/topic/183116-phpmysql-delete-row-script/#findComment-966425 Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2009 Share Posted November 27, 2009 Your code is dependent on register_globals being ON to magically populate $cmd and $id from the $_GET variables on the end of the URL. Where ever you found or learned that code, it is 7 years out of date because register_globals were turned off by default in php4.2 in April of the year 2002 and no new code, books, or tutorials should have been written after that point in time that relied on register_globals being on. You should also be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will help you. You will save a ton of time. There would have been undefined error messages concerning $cmd and $id that would have alerted you to the fact that they are not being set. Quote Link to comment https://forums.phpfreaks.com/topic/183116-phpmysql-delete-row-script/#findComment-966431 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.