Jump to content

Recommended Posts

Hi there,

 

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??

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/109543-select-all-then-delete/
Share on other sites

this should help...

 

switch(@$_GET["action"]) {
case "delete";
<?php
if(is_array($_POST['checkbox'])){
  foreach($_POST['checkbox'] as $'recordID'){
    delete mysql here
  }
}
break;
default:
html boxes in yoru loop
<form action="?action=delete">
<input class=\"blank\" name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"".$row['recordID']." \">
break;
}
?>

well in your loop of the records

 

add

echo "<input class="blank\" name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"".$row['recordID']." \">"

 

this will put a checkbox next to each of the rows, the value of these checkboxes must be set to the vlaue of the the record id

 

then have all of yoru records inside a form with the action set to "?action=delte" you may want to add a php self script in here

 

then using the case slected method

 

switch(@$_GET["action"]) {
case "delete";
<?php
if(is_array($_POST['checkbox'])){
  foreach($_POST['checkbox'] as $'recordID'){
    mysql_query("Delete From tblNAME WHERE ID='".$'recordID'."'") or die(mysql_error());
  }
}
break;

 

the code will loop through all checkboxes that are ticked take the ids from them and delete them

So would i put a delete button at the top hyperlinked to say delete.php and in that script include...

 

switch(@$_GET["action"]) {
case "delete";
<?php
if(is_array($_POST['checkbox'])){
  foreach($_POST['checkbox'] as $'recordID'){
    mysql_query("Delete From tblNAME WHERE ID='".$'recordID'."'") or die(mysql_error());
  }
}
break;

 

...is that right?

Bump!

 

Ive put the checkbox in for every message output in my while loop. I don't quite understand what the lad above is saying (although i do really appreciate your help). Im not sure what i shoudl be setting as the attributes on his line of code or what to do to create the delete button/link that wil delete the selected messages.

 

Can anyone help??

	echo "<table>\n";       
        echo "<tr><th>Message ID</th><th>Subject</th><th>Delete</th></tr>\n"; 
        while ($rows = mysql_fetch_array($messages)) 
        { 
			printf("<tr><td align='center' width='100'><font face='arial'><font color='#648CC8'>%s</font></td>\n", 
				$row["id"]);

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

				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"; 

 

and now im not sure where to put the delete or what to set

here you go

 

have a look at what ive added and you should be able to figure out how it works

 

<?php

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

echo '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=delete">'."\n";
	echo "<table>\n";       
        echo "<tr><th>Message ID</th><th>Subject</th><th>Delete</th></tr>\n"; 
        while ($rows = mysql_fetch_array($messages)) 
        { 
			printf("<tr><td align='center' width='100'><font face='arial'><font color='#648CC8'>%s</font></td>\n", 
				$row["id"]);

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

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


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

this instead

 

next to you acttion you had ''' needed to be "'

 

echo "<br>";
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";       

well where this code is swap tablename for you table name if you havent already done

 

does the mysql output any errors when you press the submit button?

 

Case "delete":
if(is_array($_POST['checkbox'])){
  foreach($_POST['checkbox'] as $RecordID){
    mysql_query("DELETE FROM tablename WHERE RecordID='$RecordID'") or die(mysql_error());
  }
}
echo 'records delteed';
break;

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.