stonelord Posted June 18, 2008 Share Posted June 18, 2008 Hi, I am new to programming in php and trying to learn by creating my own database interactive website. I cannot get my delete button to delete each record, it says that it did! but it doesn't. Here is my link that I created to create a button for every record that appears from the database; echo '<tr><td>'; echo $row['Department']; echo '</td><td>'; echo $row['Name']; echo '</td><td>'; echo $row['Phone']; echo '</td><td>'; echo $row['DC']; echo '</td><td>'; echo $row['Email']; echo '</td><td>'; echo $row['Notes']; echo '</td><td>'; echo '<form method="post" action="delete_contact.php">'; echo '<input type="submit" name="delete" value="Delete Record">'; echo '</form>'; echo '</td></tr>'; Now this is the code for my delete_contact.php file; <? $id=$_POST['delete']; //connect to DB and insert data mysql_connect("localhost", "my_db", "db") or die('I cannot connect to data base because: '.mysql_error()); mysql_select_db("my_db") or die(mysql_error()); mysql_query("DELETE FROM `ci_testing` WHERE $id"); Print "Your information has been successfully deleted from the database."; ?> Any help will be greatly appreciate it! Thank you Link to comment https://forums.phpfreaks.com/topic/110681-trying-to-delete-a-record/ Share on other sites More sharing options...
mbradley Posted June 18, 2008 Share Posted June 18, 2008 it looks like u are just deleteing the phrase "delete" u would have to have somthing to id the row and such that u are deleting unless u are just tring to show what u are doing ? from a code i have been writing: $sql = "DELETE FROM lslstore WHERE vname = '$key' AND agent_id = '$owner_id'"; $result = mysql_query($sql) or die(mysql_error()); echo 'Deleted...'; right now ur code $id = "delete".... Link to comment https://forums.phpfreaks.com/topic/110681-trying-to-delete-a-record/#findComment-567811 Share on other sites More sharing options...
DarkWater Posted June 18, 2008 Share Posted June 18, 2008 Change: mysql_query("DELETE FROM `ci_testing` WHERE $id"); To: mysql_query("DELETE FROM `ci_testing` WHERE $id") OR die(mysql_error()); Post the error you get. Link to comment https://forums.phpfreaks.com/topic/110681-trying-to-delete-a-record/#findComment-567814 Share on other sites More sharing options...
stonelord Posted June 18, 2008 Author Share Posted June 18, 2008 Ok, this is the error I get. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Delete Record' at line 1 Link to comment https://forums.phpfreaks.com/topic/110681-trying-to-delete-a-record/#findComment-567819 Share on other sites More sharing options...
DarkWater Posted June 18, 2008 Share Posted June 18, 2008 Well, look: $_POST['delete'] contains "Delete Record", which you're then assigning to $id and appending to your query. You need to fix your logic on that. Link to comment https://forums.phpfreaks.com/topic/110681-trying-to-delete-a-record/#findComment-567824 Share on other sites More sharing options...
mbradley Posted June 18, 2008 Share Posted June 18, 2008 is that the whole code or part of it? and do u have records in there.... what you should do.... is make it like this use that same form ... but also if u have this printing out each row in a loop i would add that to the loop and for name... put that ID = to a value ... Example: echo "<table border='1'>"; echo "<tr><th>Name</th><th>UUID</th><th>Distance</th><th>Time</th><th>sensor</th><th>SIM</th><th>Owner</th><th>delete info</tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td><center>"; echo $row[vname]; echo "</center></td><td>"; echo $row[vuuid]; echo "</td><td>"; echo $row[vdist]; echo "</td><td>"; echo $row[vtime]; echo "</td><td>"; echo $row[vsensor]; echo "</td><td>"; echo $row[vsim]; echo "</td><td>"; echo $row[vowner]; echo "</td><td>"; echo '<form method="post" action="delete_contact.php">'; echo '<input type="submit" name="delete" value=$row[vname]>'; echo '</form>'; echo "</td></tr>"; } echo "</table><center><br><br>"; echo 'Created for us with Second Life<br>Saladin NightFire<br>Wolf Creations'; echo "</center>"; Link to comment https://forums.phpfreaks.com/topic/110681-trying-to-delete-a-record/#findComment-567827 Share on other sites More sharing options...
stonelord Posted June 18, 2008 Author Share Posted June 18, 2008 OK, I GOT IT !!! ufff!!! man that was hard (at least for me). I don't understand it completely but it works great!; echo '</td><td>'; echo '<a href="delete_contact.php?id='.$row['id'].'">Delete Record</a>'; echo '</td></tr>'; And this is my delete_contact.php file code; <?PHP //connect to DB and insert data mysql_connect("localhost", "my_db", "db") or die('I cannot connect to data base because: '.mysql_error()); mysql_select_db("ci_testing") or die(mysql_error()); $id=$_GET['id']; mysql_query("DELETE FROM `PhoneList` WHERE id='$id'") OR die(mysql_error()); echo "Your information has been successfully deleted from the database"; mysql_close() ?> Thank you for your help, you forced me to think and hope to become a good programmer so that I too can help people out! thanks again Link to comment https://forums.phpfreaks.com/topic/110681-trying-to-delete-a-record/#findComment-567857 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.