pocobueno1388 Posted July 26, 2006 Share Posted July 26, 2006 I created a script where users can send messages to eachother back and forth, like a PM type thing. I have it so it displays all the messages in their inbox and has a checkbox that if they check it and press delete it is supposed to delete the message. The problem I am having is it won't delete the message when the click delete. Here is the script:[code]<?include 'header.php';if ($_POST['delete']){ mysql_query("DELETE * FROM message WHERE messageID='$delete'");}print<<<HERE<form action="inbox.php?action=delete"><center><h2>INBOX</h2><p><h3><a href="sendmessage.php">Send a message</a></h3></center><table border=1 bordercolor="black" width="85%" align="center"><th align="center" bgcolor="49614A">Delete?</th><th align="center" bgcolor="49614A">Subject</th><th align="center" bgcolor="49614A">From</th><th align="center" bgcolor="49614A">Time</th><th align="center" bgcolor="49614A">Read?</th></tr>HERE;$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";$result1= mysql_query($sql1);$row1 = mysql_fetch_assoc($result1);$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";$result= mysql_query($sql);while ($row = mysql_fetch_assoc($result)){ echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete"></td>'; echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' . $row['subject'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' . $row['sender'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';if ($row['read'] == no){ echo '<td align="center"><font color="red">' . $row['read'] . '</font></td></tr>';} else {echo '<td align="center" bgcolor="788D7A"><font color="C8F6C8">' . $row['read'] . '</font></td></tr>';}//end if}//end whileprint<<<HERE</table><p><table width="85%" align="center"><td align="left"><input type="submit" name="delete" value="delete"></td></table></form>HERE;?>[/code]The delete boxes and codes are all in different spots in the script, so I thought it would be easier if I just posted the entire script. Any help would be appreciated =D Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/ Share on other sites More sharing options...
High_-_Tek Posted July 26, 2006 Share Posted July 26, 2006 if ($_POST['delete']){ mysql_query("DELETE * FROM message WHERE messageID='$delete'");}toif ($_POST['delete']){ mysql_query("DELETE * FROM message WHERE messageID='".$_POST['delete']."');} Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63803 Share on other sites More sharing options...
pocobueno1388 Posted July 26, 2006 Author Share Posted July 26, 2006 High Tek - I switched my code to your code and the screen went blank, so I tried this:[code]if ($_POST['delete']){ mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");}[/code]The screen came back, but it still won't delete the message =( Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63810 Share on other sites More sharing options...
corbin Posted July 26, 2006 Share Posted July 26, 2006 Try adding echo "<form action=\"\" method=POST>"; above while ($row = mysql_fetch_assoc($result)){ Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63814 Share on other sites More sharing options...
pocobueno1388 Posted July 26, 2006 Author Share Posted July 26, 2006 Corbin - Nope =/ That didn't work either. Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63826 Share on other sites More sharing options...
redarrow Posted July 26, 2006 Share Posted July 26, 2006 post the code you got so far and add the database lol..........That a terrable way to delete can not beleve it lol..............just add a link were the delete button is of exampleYou got to make a link but do the link to suit your needs ok.echo"<a href='delete.php'?playerID=$row[senderID]&cmd=delete'>delete</a>":delete.php<?database connectionif($_GET[cmd]==$delete) {$query="DELETE * FROM message WHERE playerID=$row[senderID]");$result_mysql($query);heder("localtion: back_to_where_ever.php";}?> this is an example only but should work with the correct delete settings and id on the link.good luck.my way or the high way lol................ Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63827 Share on other sites More sharing options...
pocobueno1388 Posted July 26, 2006 Author Share Posted July 26, 2006 Code:[code]<?include 'header.php';if ($_POST['delete']){ mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");}print<<<HERE<form action="inbox.php?action=delete" method="post"><center><h2>INBOX</h2><p><h3><a href="sendmessage.php">Send a message</a></h3></center><table border=1 bordercolor="black" width="85%" align="center"><th align="center" bgcolor="49614A">Delete?</th><th align="center" bgcolor="49614A">Subject</th><th align="center" bgcolor="49614A">From</th><th align="center" bgcolor="49614A">Time</th><th align="center" bgcolor="49614A">Read?</th></tr>HERE;$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";$result1= mysql_query($sql1);$row1 = mysql_fetch_assoc($result1);$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";$result= mysql_query($sql);while ($row = mysql_fetch_assoc($result)){ echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete"></td>'; echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' . $row['subject'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' . $row['sender'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';if ($row['read'] == no){ echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] . '</font></td></tr>';} else {echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] . '</font></td></tr>';}//end if}//end whileprint<<<HERE</table><p><table width="85%" align="center"><td align="left"><input type="submit" name="delete" value="delete"></td></table></form>HERE;?>[/code]I don't think there is a need for the database stuff...there should be enough information there. Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63831 Share on other sites More sharing options...
vinny_bc Posted July 26, 2006 Share Posted July 26, 2006 Your checkbox should be of the form:<input type="checkbox" name="delete[]" value="$messageID">Then $_POST['delete'] will be an array of checked $messageIDs.Note: if the user didn't check anything, then calling isset($_POST['delete']) will return FALSE. Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63833 Share on other sites More sharing options...
pocobueno1388 Posted July 26, 2006 Author Share Posted July 26, 2006 Vinny_bc - That didn't work either =/Here is the updated code:[code]<?include 'header.php';if ($_POST['delete']){ mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");}print<<<HERE<form action="inbox.php?action=delete" method="post"><center><h2>INBOX</h2><p><h3><a href="sendmessage.php">Send a message</a></h3></center><table border=1 bordercolor="black" width="85%" align="center"><th align="center" bgcolor="49614A">Delete?</th><th align="center" bgcolor="49614A">Subject</th><th align="center" bgcolor="49614A">From</th><th align="center" bgcolor="49614A">Time</th><th align="center" bgcolor="49614A">Read?</th></tr>HERE;$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";$result1= mysql_query($sql1);$row1 = mysql_fetch_assoc($result1);$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";$result= mysql_query($sql);while ($row = mysql_fetch_assoc($result)){ echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete[]" value='. $row['messageID'] .'></td>'; echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' . $row['subject'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' . $row['sender'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';if ($row['read'] == no){ echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] . '</font></td></tr>';} else {echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] . '</font></td></tr>';}//end if}//end whileprint<<<HERE</table><p><table width="85%" align="center"><td align="left"><input type="submit" name="delete" value="delete"></td></table></form>HERE;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63837 Share on other sites More sharing options...
redarrow Posted July 26, 2006 Share Posted July 26, 2006 what does the ecohed delete query say please cheers. Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63839 Share on other sites More sharing options...
vinny_bc Posted July 26, 2006 Share Posted July 26, 2006 You will need to change your SQL code from:if ($_POST['delete']){ mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");}To:if(isset($_POST['delete'])){ foreach($_POST['delete'] as $i => $message_id){ mysql_query("DELETE * FROM message WHERE messageID='$message_id'"); }}Remember, $_POST['delete'] is now an array and not a scalar value.If this doesn't work either, can you please post the HTML that is output by your script - there may still be issues with how your form is being output as HTML.Cheers Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63841 Share on other sites More sharing options...
redarrow Posted July 26, 2006 Share Posted July 26, 2006 correct i want to see your delete querythe where cluase might and think is wrong ok.you can make it a lot simple then above use a session example<? session_start();$id=$_SESSION['id']=$row['message_id'];then in the delete where clause use this okif(isset($_POST['delete'])){ mysql_query("DELETE * FROM message WHERE messageID='$id'"); }} Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63843 Share on other sites More sharing options...
pocobueno1388 Posted July 26, 2006 Author Share Posted July 26, 2006 vinney_bc - That didn't work either =/redarrow - I don't think I want to do the session idea. It is to confusing, and I already have sessions set up in the header file.New code:[code]<?include 'header.php';if(isset($_POST['delete'])){ foreach($_POST['delete'] as $i => $message_id){ mysql_query("DELETE * FROM message WHERE messageID='$row[messageID]'"); }}print<<<HERE<form action="inbox.php?action=delete" method="post"><center><h2>INBOX</h2><p><h3><a href="sendmessage.php">Send a message</a></h3></center><table border=1 bordercolor="black" width="85%" align="center"><th align="center" bgcolor="49614A">Delete?</th><th align="center" bgcolor="49614A">Subject</th><th align="center" bgcolor="49614A">From</th><th align="center" bgcolor="49614A">Time</th><th align="center" bgcolor="49614A">Read?</th></tr>HERE;$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";$result1= mysql_query($sql1);$row1 = mysql_fetch_assoc($result1);$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";$result= mysql_query($sql);while ($row = mysql_fetch_assoc($result)){ echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete[]" value='. $row['messageID'] .'></td>'; echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' . $row['subject'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' . $row['sender'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';if ($row['read'] == no){ echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] . '</font></td></tr>';} else {echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] . '</font></td></tr>';}//end if}//end whileprint<<<HERE</table><p><table width="85%" align="center"><td align="left"><input type="submit" name="delete" value="delete"></td></table></form>HERE;?>[/code]What do you guys mean by my 'delete query'? Is it now in the script? Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63845 Share on other sites More sharing options...
vinny_bc Posted July 26, 2006 Share Posted July 26, 2006 You didn't follow my code exactly :)You had:[code]if(isset($_POST['delete'])){ foreach($_POST['delete'] as $i => $message_id){ mysql_query("DELETE * FROM message WHERE messageID='[color=red][b]$row[messageID][/b][/color]'"); }}[/code]Whereas I had:[code]if(isset($_POST['delete'])){ foreach($_POST['delete'] as $i => $message_id){ mysql_query("DELETE * FROM message WHERE messageID='[b]$message_id[/b]'"); }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-63848 Share on other sites More sharing options...
pocobueno1388 Posted July 26, 2006 Author Share Posted July 26, 2006 Nope =/ That didn't work either. Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64178 Share on other sites More sharing options...
pocobueno1388 Posted July 26, 2006 Author Share Posted July 26, 2006 Anyone else have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64343 Share on other sites More sharing options...
vinny_bc Posted July 26, 2006 Share Posted July 26, 2006 can you post your code as it stands at the moment.And can you also give us a sample of your DB. Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64387 Share on other sites More sharing options...
pocobueno1388 Posted July 27, 2006 Author Share Posted July 27, 2006 My code is where exactly how it is when I first posted. There were so many ideas flying around, and I couldn't get any to work, so I guess we will have to start fresh again. So here is the code again:[code]<?include 'header.php';if ($_POST['delete']){ mysql_query("DELETE * FROM message WHERE messageID='$delete'");}print<<<HERE<form action="inbox.php?action=delete"><center><h2>INBOX</h2><p><h3><a href="sendmessage.php">Send a message</a></h3></center><table border=1 bordercolor="black" width="85%" align="center"><th align="center" bgcolor="49614A">Delete?</th><th align="center" bgcolor="49614A">Subject</th><th align="center" bgcolor="49614A">From</th><th align="center" bgcolor="49614A">Time</th><th align="center" bgcolor="49614A">Read?</th></tr>HERE;$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";$result1= mysql_query($sql1);$row1 = mysql_fetch_assoc($result1);$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";$result= mysql_query($sql);while ($row = mysql_fetch_assoc($result)){ echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete"></td>'; echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' . $row['subject'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' . $row['sender'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';if ($row['read'] == no){ echo '<td align="center"><font color="red">' . $row['read'] . '</font></td></tr>';} else {echo '<td align="center" bgcolor="788D7A"><font color="C8F6C8">' . $row['read'] . '</font></td></tr>';}//end if}//end whileprint<<<HERE</table><p><table width="85%" align="center"><td align="left"><input type="submit" name="delete" value="delete"></td></table></form>HERE;?>[/code]Here is the database information:Table name - messageFields:messageIDsenderreceiversenderIDsubjectbodytimeread Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64498 Share on other sites More sharing options...
sanfly Posted July 27, 2006 Share Posted July 27, 2006 Okay, I see two things wrong here. Of course I could be completely wrong on either of these, but anyway....1. In the delete query[code]mysql_query("DELETE * FROM message WHERE messageID='$delete'");[/code]you dont need the *, infact if i remember correctly, it prevents the delete query from working. 2. In the checkbox to delete, I dont think your passing the value for the messageID[code]<input type="checkbox" name="delete">[/code]I dont use checkboxes that often, I may be wrong here, but try adding the messageID as the value[code]<input type="checkbox" name="delete" value="' . $row['messageID'] . '">[/code]Also, Im assuming this is generating a list of messages, and you can select more than one to delete? If this is the case, you will need to change the delete checkbox to this:[code]<input type="checkbox" name="delete[]" value="' . $row['messageID'] . '">[/code]([] after the name, puts the values into an array)and then when you submit the form, you need to extract the values as an array, and use either a for or foreach loop (I always have trouble with foreach, and forget when it can and cant be used so it might be better to use count() to find out how many items in the array, then use a for loop) to delete each oneROUGH EXAMPLE, WILL NEED REFINING, IS ONLY HERE TO GIVE YOU AN IDEA:[code]if ($_POST['delete']){ $delMessages = $_POST['delete']; // $delMessages or maybe $delMessages[] ????? $num = count($delMessages); for($i = 0; $i < $num; $i++){ $messageID = $delMessages[$i]; // not so sure on this.... mysql_query("DELETE FROM message WHERE messageID = '$messageID'"); }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64511 Share on other sites More sharing options...
pocobueno1388 Posted July 27, 2006 Author Share Posted July 27, 2006 Sanfly - I put in everything you had put, and I couldn't get it to work. Here is the code with all your stuff added:[code]<?include 'header.php';if ($_POST['delete']){ $delMessages = $_POST['delete']; // $delMessages or maybe $delMessages[] ????? $num = count($delMessages); for($i = 0; $i < $num; $i++){ $messageID = $delMessages[$i]; // not so sure on this.... mysql_query("DELETE FROM message WHERE messageID = '$messageID'"); }}print<<<HERE<form action="inbox.php?action=delete" method="post"><center><h2>INBOX</h2><p><h3><a href="sendmessage.php">Send a message</a></h3></center><table border=1 bordercolor="black" width="85%" align="center"><th align="center" bgcolor="49614A">Delete?</th><th align="center" bgcolor="49614A">Subject</th><th align="center" bgcolor="49614A">From</th><th align="center" bgcolor="49614A">Time</th><th align="center" bgcolor="49614A">Read?</th></tr>HERE;$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";$result1= mysql_query($sql1);$row1 = mysql_fetch_assoc($result1);$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";$result= mysql_query($sql);while ($row = mysql_fetch_assoc($result)){ echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete[]" value="' . $row['messageID'] . '"></td>'; echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' . $row['subject'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' . $row['sender'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';if ($row['read'] == no){ echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] . '</font></td></tr>';} else {echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] . '</font></td></tr>';}//end if}//end whileprint<<<HERE</table><p><table width="85%" align="center"><td align="left"><input type="submit" name="delete" value="delete"></td></table></form>HERE;?>[/code]Here is the original code again, if anyone else would like to give it a shot:[code]<?include 'header.php';if ($_POST['delete']){ mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");}print<<<HERE<form action="inbox.php?action=delete" method="post"><center><h2>INBOX</h2><p><h3><a href="sendmessage.php">Send a message</a></h3></center><table border=1 bordercolor="black" width="85%" align="center"><th align="center" bgcolor="49614A">Delete?</th><th align="center" bgcolor="49614A">Subject</th><th align="center" bgcolor="49614A">From</th><th align="center" bgcolor="49614A">Time</th><th align="center" bgcolor="49614A">Read?</th></tr>HERE;$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";$result1= mysql_query($sql1);$row1 = mysql_fetch_assoc($result1);$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";$result= mysql_query($sql);while ($row = mysql_fetch_assoc($result)){ echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete"></td>'; echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' . $row['subject'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' . $row['sender'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';if ($row['read'] == no){ echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] . '</font></td></tr>';} else {echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] . '</font></td></tr>';}//end if}//end whileprint<<<HERE</table><p><table width="85%" align="center"><td align="left"><input type="submit" name="delete" value="delete"></td></table></form>HERE;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64522 Share on other sites More sharing options...
vinny_bc Posted July 27, 2006 Share Posted July 27, 2006 I think u'll need to provide us with some debug messages so that we can figure out what's going onTry modifying the code to this so that it's outputting some debug statements. Then copy and paste the statements into a reply so that we can have a look.[code=php:0]if ($_POST['delete']){ $delMessages = $_POST['delete']; // $delMessages or maybe $delMessages[] ????? $num = count($delMessages); for($i = 0; $i < $num; $i++){ $messageID = $delMessages[$i]; // not so sure on this.... $sql = "DELETE FROM message WHERE messageID = '$messageID'"; echo $sql.'<BR><BR>'; mysql_query($sql); }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64525 Share on other sites More sharing options...
pocobueno1388 Posted July 27, 2006 Author Share Posted July 27, 2006 Here is the error I got:[code]DELETE FROM message WHERE messageID = 'd'[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64528 Share on other sites More sharing options...
vinny_bc Posted July 27, 2006 Share Posted July 27, 2006 is messageID meant to be a letter or a number?If it's meant to be a number, then the problem is you are writing the wrong row value as the value of your checkbox.If it's meant to be a letter, than can you provide us with some sample data from your table.thanks. Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64529 Share on other sites More sharing options...
pocobueno1388 Posted July 27, 2006 Author Share Posted July 27, 2006 Woah, I got it to delete messages now, it just only deletes one message at a time. Here is the code:[code]<?include 'header.php';if ($_POST['delete']){ $delMessages = $_POST['delete']; // $delMessages or maybe $delMessages[] ????? $num = count($delMessages); for($i = 0; $i < $num; $i++){ $sql6 = "SELECT `messageID` FROM message WHERE receiver='$sid'";$result6= mysql_query($sql6);$row6 = mysql_fetch_assoc($result6); $messageID = $row6['messageID']; // not so sure on this.... $sql = "DELETE FROM message WHERE messageID = '$messageID'"; echo $sql.'<BR><BR>'; mysql_query($sql); }}print<<<HERE<form action="inbox.php?action=delete" method="post"><center><h2>INBOX</h2><p><h3><a href="sendmessage.php">Send a message</a></h3></center><table border=1 bordercolor="black" width="85%" align="center"><th align="center" bgcolor="49614A">Delete?</th><th align="center" bgcolor="49614A">Subject</th><th align="center" bgcolor="49614A">From</th><th align="center" bgcolor="49614A">Time</th><th align="center" bgcolor="49614A">Read?</th></tr>HERE;$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";$result1= mysql_query($sql1);$row1 = mysql_fetch_assoc($result1);$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";$result= mysql_query($sql);while ($row = mysql_fetch_assoc($result)){ echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete[]" value="' . $row['messageID'] . '"></td>'; echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' . $row['subject'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' . $row['sender'] . '</a></td>'; echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';if ($row['read'] == no){ echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] . '</font></td></tr>';} else {echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] . '</font></td></tr>';}//end if}//end whileprint<<<HERE</table><p><table width="85%" align="center"><td align="left"><input type="submit" name="delete" value="delete"></td></table></form>HERE;?>[/code]Any ideas on how to get it to delete every message that is checkmarked? Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64531 Share on other sites More sharing options...
sanfly Posted July 27, 2006 Share Posted July 27, 2006 That code you wrote is not even taking the checked items into account, its probably just deleting the first message whether selected or not, right?Try replacing that if statement with this[code]if ($_POST['delete']){ echo "We've selected some items to delete<br><br>"; // Debug code, can be removed later $delMessages = $_POST['delete']; // Get the array echo "ARRAY: "; print_r($delMessages); echo "<br><br>"; // dislaying the array for us, can be removed later $num = count($delMessages); // Count how many items in the array for($i = 0; $i < $num; $i++){ // beginning of the for loop to run through all the array items $messageID = $delMessages[$i]; // Sets the variable $messageID for the selected message $r = mysql_query("DELETE FROM message WHERE messageID = '$messageID'") or die(mysql_error()); // you should always include a die statement when trying to debug mysql echo "$r<br><br>"; // display the query so we can see if there is anything going wrong, can be removed later }}else{ echo "Nothing Selected for deletion<br><br>";}[/code]copy and paste exactly what show up on the next page Quote Link to comment https://forums.phpfreaks.com/topic/15651-deleting-a-row-from-the-database-solved/#findComment-64543 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.