perky416 Posted March 12, 2011 Share Posted March 12, 2011 Hi Everyone, Im trying to add a delete button to the message inbox page of my website, where when delete is clicked, the checked messages get deleted from the database. After a bit of research on google i have come up with the code below however i cant get it to work. I have replaced the mysql delete query with echo "checked"; just for debugging. $query = mysql_query("SELECT * FROM users WHERE id='$id'"); $row = mysql_fetch_assoc($query); $submit = $_POST['submit']; $username = $row['username']; $checked = $_POST['message']; $messages_query = mysql_query("SELECT * FROM messages WHERE recipient='$username'"); if (mysql_num_rows($messages_query) > 0) { while ($messages_row = mysql_fetch_array($messages_query)) { if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; } $message_id = $messages_row['id']; echo "<a href='message.php?id=$message_id'>"; echo "From: " . $messages_row['sender']; echo "Subject: " . $messages_row['subject'] . "<br />"; echo "</a>"; echo "<form method='POST'><input type='checkbox' name='message[]' value='1' /></form>"; if ($messages_row['message_read'] == 0) { echo "</div>"; } } } else { echo "No messages"; } if ($submit) { if ($checked == "1") { echo "checked"; } } ?> <html> <form method="POST"> <input type="submit" name="submit" value="Delete" /> </form> </html> I have also tried the following but i still cant get it to work: if (isset($checked)) { echo "checked"; } Can anybody see where iv gone wrong? Thanks Link to comment https://forums.phpfreaks.com/topic/230437-deleting-database-row-using-checkbox/ Share on other sites More sharing options...
Pikachu2000 Posted March 12, 2011 Share Posted March 12, 2011 You can't split up the <form> tag like that, it won't work that way. Link to comment https://forums.phpfreaks.com/topic/230437-deleting-database-row-using-checkbox/#findComment-1186661 Share on other sites More sharing options...
perky416 Posted March 12, 2011 Author Share Posted March 12, 2011 Hi Pikachu Does this look better: $query = mysql_query("SELECT * FROM users WHERE id='$id'"); $row = mysql_fetch_assoc($query); $submit = $_POST['submit']; $username = $row['username']; $checked = $_POST['message']; $messages_query = mysql_query("SELECT * FROM messages WHERE recipient='$username'"); if (mysql_num_rows($messages_query) > 0) { while ($messages_row = mysql_fetch_array($messages_query)) { if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; } $message_id = $messages_row['id']; echo "<a href='message.php?id=$message_id'>"; echo "From: " . $messages_row['sender']; echo "Subject: " . $messages_row['subject'] . "<br />"; echo "</a>"; echo "<form method='POST'><input type='checkbox' name='message[]' value='1' />"; if ($messages_row['message_read'] == 0) { echo "</div>"; } } } else { echo "No messages"; } if ($submit) { if ($checked == "1") { echo "checked"; } } ?> <html> <input type="submit" name="submit" value="Delete" /> </form> </html> I can get the echo "checked"; to display when i change the <input type='checkbox' name='message[]' to <input type='checkbox' name='message', however with name='message[]' its just not working, its racking my brain iv been trying to figure out what is wrong all afternoon. Thanks Link to comment https://forums.phpfreaks.com/topic/230437-deleting-database-row-using-checkbox/#findComment-1186666 Share on other sites More sharing options...
perky416 Posted March 12, 2011 Author Share Posted March 12, 2011 Iv been having a play but i still cant get it. When i use the following i can successfully echo the id of the message that has been checked. $query = mysql_query("SELECT * FROM users WHERE id='$id'"); $row = mysql_fetch_assoc($query); $submit = $_POST['submit']; $username = $row['username']; $checked = $_POST['message']; $checkname = $_POST['checkname']; $messages_query = mysql_query("SELECT * FROM messages WHERE recipient='$username'"); if (mysql_num_rows($messages_query) > 0) { while ($messages_row = mysql_fetch_array($messages_query)) { if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; } $message_id = $messages_row['id']; echo "<a href='message.php?id=$message_id'>"; echo "From: " . $messages_row['sender']; echo "Subject: " . $messages_row['subject'] . "<br />"; echo "</a>"; echo "<form method='POST'><input type='checkbox' name='checkname[]' value='$message_id' />"; if ($messages_row['message_read'] == 0) { echo "</div>"; } } } else { echo "No messages"; } if ($submit) { if(isset($checkname)) { echo implode($checkname); } } ?> <html> <input type="submit" name="submit" value="Delete" /> </form> </html> However when i change the echo to mysql_query("DELETE FROM messages WHERE id='$message_id'"); and click delete, the message with the latest id gets deleted. Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/230437-deleting-database-row-using-checkbox/#findComment-1186679 Share on other sites More sharing options...
DavidAM Posted March 12, 2011 Share Posted March 12, 2011 Move your FORM open tag outside of the WHILE loop. if (mysql_num_rows($messages_query) > 0) { echo "<form method='POST'>"; while ($messages_row = mysql_fetch_array($messages_query)) { if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; } $message_id = $messages_row['id']; echo "<a href='message.php?id=$message_id'>"; echo "From: " . $messages_row['sender']; echo "Subject: " . $messages_row['subject'] . "<br />"; echo "</a>"; echo "<input type='checkbox' name='message[]' value='1' />"; if ($messages_row['message_read'] == 0) { echo "</div>"; } } } But with this code you cannot tell WHICH checkbox was checked. Change the CHECKBOX line to : echo "<input type='checkbox' name='message[]' value='$message_id' />"; Then you can walk the array of checkboxes with something like this: psuedo-code foreach ($_POST['message'] as $msgID) { DELETE FROM messages WHERE id = $msgID; } Link to comment https://forums.phpfreaks.com/topic/230437-deleting-database-row-using-checkbox/#findComment-1186682 Share on other sites More sharing options...
perky416 Posted March 12, 2011 Author Share Posted March 12, 2011 Thanks david that worked a treat! I never thought to use a foreach. Its all a big learning curve. Thanks! Link to comment https://forums.phpfreaks.com/topic/230437-deleting-database-row-using-checkbox/#findComment-1186694 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.