Jump to content

Delete messages


coolphpdude

Recommended Posts

I started a post off a few weeks ago about this, but i have left it, avoided it, and put it to the back of my list of jobs because i wasn't having much luck with it. So any help would be appreciated.

 

i think this will be an easy one for you lot but i just can't seem to get my head around it. I got a message database on my project. So a user receives messages from other user's just like an email system. Currently i have a delete button next to each message as it is output from the database using a while loop. Now the propblem with this is, if you have got 40 messages you have to click delete 40 times. I want it so that theres a tick box next to each message so u can mark the messages you would like to delete and click a single delete button at the top to delete all of the marked messages. Is this easy? most email messaging systems have it so i suppose its do able, i just dont know how. Also could i place a tick box at the so the user could click this and it would mark all messages on this page??

 

what i've got so far is with the help of jesushax which i am very grateful of. I'll post the code below...

 


<?php
session_start();

switch(@$_GET["action"]) {
Case "delete":
if(is_array($_POST['checkbox'])){
  foreach($_POST['checkbox'] as $message){
    mysql_query("DELETE FROM memos WHERE message_id='$message'") or die(mysql_error());
  }
}
//echo 'records deleted';
//break;
default:

echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'?action=delete">';
	echo "<table width='100%' border='2' bordercolor='#cccccc' style='border-collapse: collapse' cellpadding='5' align=center >\n";       
        echo "<tr><th>Subject</th><th>Sender</th><th>Delete</th></tr>\n"; 
        while ($memos = mysql_fetch_array($memos)) 
        { 

				printf("<td>%s</td>\n", $memos["memo_subject"]);

				printf("<td>%s</td>\n", 
				$my_message_row["message_sender"]);

				printf("<td align='center' cellpadding='10' width='60'><input class='blank' name='checkbox[]' type='checkbox' id='checkbox[]' value='".$row['recordID']." '></td></tr>"); 
         } 
         echo "</table>\n"; 

echo '<tr><td colspan="3"><input type="submit" value="Delete these records" />'."\n";
echo "</form>"; 

?>

 

Cheers

Link to comment
Share on other sites

The code you have looks like it's most of the way there.  What exactly isn't working?  What have you done to try and troubleshoot it?

 

Do you understand the concepts behind the code the other person helped you with?  Because once you understand the concept the steps for troubleshooting are always the same:

 

1)  Know what data to expect

2)  Look at what you have

 

Are 1 and 2 the same?

 

Yes - Your code works (assuming you got #1 correct).

No - What did you get that you didn't expect and why?

Link to comment
Share on other sites

Hi sorry for not replying sooner, i got pulled away from working on this.

 

So far i played about with the positioning of the code as when we originally started it was producing lots of errors. Now, theres no errors but when you tick a box and cclick the delete button, the page kind of refreshes but nothing is deleted...

Link to comment
Share on other sites

u have not mentioned ....$row['recordID']  
change it as $memos['recordID']

				printf("<td align='center' cellpadding='10' width='60'><input class='blank' name='checkbox[]' type='checkbox' id='checkbox[]' value='".$memos['recordID']." '></td></tr>"); 
         } 
         echo "</table>\n"; 


Link to comment
Share on other sites

When I do these types of things for my stuff, I need to give each input field a unique name. I'm not sure what you're doing by calling it "checkbox[]', I would suggest "cb$row[recordID]" as to give each a unique name then step through each using current() and next()

Link to comment
Share on other sites

@dannyb785

He's calling it checkbox[] to place them into a numerically indexed array named $checkbox.  He's distinguishing the IDs in the value attribute of the checkbox.  Basically, the method he has should work fine if he can sort things out on the back end.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.