Ty44ler Posted August 21, 2009 Share Posted August 21, 2009 I have a site where users submit information that is displayed on another page. I have a delete button below the information that should delete it from my database... It says it's successful but when I check the database the records are still there... Form on the display page: $sql = "select * from users order by id DESC"; $result = mysql_query ($sql); if(!isset($cmd)) { while ($row = mysql_fetch_array($result)) { $id=$row["id"]; echo " <h2>$id</h2> <form action='delete.php' method='POST'> <input type='submit' value='delete' name='id' /> </form> Code for delete.php: // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$HTTP_POST_VARS['$id']; // Delete data in mysql from row that has this id $sql="DELETE FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); // if successfully deleted if($result){ echo "Deleted Successfully"; echo "$id"; echo "<a href='display.php'>Back to main page</a>"; } else { echo "ERROR"; } // close connection mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/171307-solved-delete-record-button-not-working/ Share on other sites More sharing options...
ignace Posted August 21, 2009 Share Posted August 21, 2009 What does print $sql say? Quote Link to comment https://forums.phpfreaks.com/topic/171307-solved-delete-record-button-not-working/#findComment-903428 Share on other sites More sharing options...
PFMaBiSmAd Posted August 21, 2009 Share Posted August 21, 2009 $HTTP_POST_VARS was depreciated over 7 years ago, turned off by default in php5, and completely removed in php6. Use $_POST instead. Your code displays the "Deleted Successfully" message if the query is executed at all. You would actually need to test the value that mysql_affected_rows() returns to know if the query deleted anything. Quote Link to comment https://forums.phpfreaks.com/topic/171307-solved-delete-record-button-not-working/#findComment-903431 Share on other sites More sharing options...
Ty44ler Posted August 21, 2009 Author Share Posted August 21, 2009 print $sql doesn't say anything. I don't think the variable is getting passed correctly. I changed the $id to an actual id number and it deletes it correctly, so I think it has to do with the variable. I also changed $HTTP_POST_VARS to $_POST (someone told me to try and change it that way to see if that was the problem). Quote Link to comment https://forums.phpfreaks.com/topic/171307-solved-delete-record-button-not-working/#findComment-903464 Share on other sites More sharing options...
PFMaBiSmAd Posted August 21, 2009 Share Posted August 21, 2009 Actually, your form is not doing anything with the id so it will be a little hard for the form processing code to know which id to use. Quote Link to comment https://forums.phpfreaks.com/topic/171307-solved-delete-record-button-not-working/#findComment-903467 Share on other sites More sharing options...
Ty44ler Posted August 21, 2009 Author Share Posted August 21, 2009 Actually, your form is not doing anything with the id so it will be a little hard for the form processing code to know which id to use. I'm kinda confused... Then how do I get it to pass the id that I want along with it? Quote Link to comment https://forums.phpfreaks.com/topic/171307-solved-delete-record-button-not-working/#findComment-903469 Share on other sites More sharing options...
Ty44ler Posted August 21, 2009 Author Share Posted August 21, 2009 I figured it out.... Here's my new code if anybody was wondering... <form action='delete.php' method='POST'> <input type='hidden' value='$id' name='id' /> <input type='submit' value='delete' name='delete' /> </form> and... // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$_REQUEST["id"]; // Delete data in mysql from row that has this id $sql="DELETE FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); // if successfully deleted if($result){ echo "$id Deleted Successfully "; echo "<a href='display.php'>Back to main page</a>"; } else { echo "ERROR"; } // close connection mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/171307-solved-delete-record-button-not-working/#findComment-903489 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.