multiple delete

delecon
delecon used Ask the Experts™
on
Someone please help me.

I need to delete multiple rows using check box select, just like yahoo webmail, there is a check box to delete the emails that you have select. On click "delete" all the rows that are selected will be deleted, how to do that?


My code:


email.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
      
$Query = "SELECT * from emailing where (((recipient = '$name') AND (folder = 'inbox'))) ORDER BY ID DESC LIMIT 25";
$Result = mysql_db_query($DBName, $Query, $Link);

while($emailrow = mysql_fetch_array($Result)){

print "<form name='del' action='deleteemail.php' method='post'>">;

print "<INPUT type='submit' value='delete'>";

print "<table><tr>";

print "<td><input type='checkbox' name='delete'></td>";

print "<td>$emailrow[sender]</td>";

print "<td>$emailrow[subject]</td>";

print "<td>$emailrow[date]</td>";

print "<INPUT type='submit' value='delete'>";

print "</table></tr>";
      
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



deleteemail.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";

$Query2 = "DELETE from emailing where (((WHAT TO PUT HERE???) AND (recipient = '$name') AND (folder = 'inbox')))";
mysql_db_query($DBName, $Query2, $Link);

?>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


My SQL DB columns

ID (auto)
sender
recipient
folder
subject
date

--------------------------------------------------------

Really need this, someone please help me, thank you very much!






Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
something that you could do is set the checkbox to an array.
then cycle that array do delete the selected messages.
the problem would be you would need to send a value with the delete.  I changed the code a bit see if this helps.
this has not been tested and I just whipped something together to give you an idea.  it should work but if you have an error just let me know.

email.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
   
$Query = "SELECT * from emailing where (((recipient = '$name') AND (folder = 'inbox'))) ORDER BY ID DESC LIMIT 25";
$Result = mysql_db_query($DBName, $Query, $Link);

while($emailrow = mysql_fetch_array($Result)){

print "<form name='del' action='deleteemail.php' method='post'>">;

print "<INPUT type='submit' name="delete" value='delete'>";

print "<table><tr>";

print "<td><input type='checkbox' name='deleterow[$emailrow[ID]]'></td>";

print "<td>$emailrow[sender]</td>";

print "<td>$emailrow[subject]</td>";

print "<td>$emailrow[date]</td>";

print "<INPUT type='submit' value='delete'>";

print "</table></tr>";
   
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



deleteemail.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
foreach($_POST['deleterow'] as $emailID) {
  $Query2 = "DELETE from emailing where ID=$emailID AND (recipient = '$name') AND (folder = 'inbox')))";
}
mysql_db_query($DBName, $Query2, $Link);

?>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
create checkboxes that contain the ID (by putting "ID[]" u create an array):

print "<INPUT type='checkbox' name='ID[]' value='$emailrow[ID]'>";

an array of all selected id's can be accessed in the deleteemail.php:

foreach ($_REQUEST["ID"] as $ID)
{
    mysql_query("DELETE from emailing where ID=$ID");
}

Commented:
There are multiple ways to do that.  One of the better ways would be that you make only one form for all returned results, and name your checkboxes for each row returned as:

<input type="checkbox" name="delete[]" value="rowautonumber"> delete<br>

Then in your php script do implode to join all the data in $_POST['delete'] array and put it in your sql delete statement (of course you need to change your current delete statement to use autonumber for deletion.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Commented:
Gee, just a couple minutes delay already two guys beat me on answering!  I should type faster I guess. :-)
woops :)
kinda messed mine up huh.. forgot to put the thing in the value section

Author

Commented:
Hi I got problem submitting the form..


some thing wrong?

<?
include ("webmail.php")  ;

$name="testing";
   
$Query = "SELECT * from emailing where (((recipient = '$name') AND (folder = 'inbox'))) ORDER BY ID DESC LIMIT 25";
$Result = mysql_db_query($DBName, $Query, $Link);

while($emailrow = mysql_fetch_array($Result)){

print "<form name='del' action='deleteemail.php' method='post'>";

print "<INPUT type='submit' name="delete" value='delete'>";

print "<table><tr>";

print "<td><input type='checkbox' name='deleterow[$emailrow[ID]]'></td>";

print "<td>$emailrow[sender]</td>";

print "<td>$emailrow[subject]</td>";

print "<td>$emailrow[date]</td>";

print "<INPUT type='submit' value='delete'>";

print "</table></tr>";
   
}



should I place print "<form name='del' action='deleteemail.php' method='post'>";
after while($emailrow = mysql_fetch_array($Result)){ or before?


and my submit button have multiple now.. I only need one..

Commented:
Should be before while, and your submit should be after the while loop, and don't forget to put a </form> tag there.  I'll see if this time I beat the other two guys. :-)
i assume you are using post variables... i switched this to request so that get post and cookie work

email.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
   
$Query = "SELECT * from emailing where (((recipient = '$name') AND (folder = 'inbox'))) ORDER BY ID DESC LIMIT 25";
$Result = mysql_db_query($DBName, $Query, $Link);

while($emailrow = mysql_fetch_array($Result)){

print "<form name='del' action='deleteemail.php' method='post'>">;

print "<INPUT type='submit' name="delete" value='delete'>";

print "<table><tr>";

print "<td><input type='checkbox' name='deleterow[]' value=\"$emailrow[ID]\"></td>";

print "<td>$emailrow[sender]</td>";

print "<td>$emailrow[subject]</td>";

print "<td>$emailrow[date]</td>";

print "</table></tr>";
   
}

print "<INPUT type='submit' value='delete'>";

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



deleteemail.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
foreach($_REQUEST['deleterow'] as $emailID) {
 $Query2 = "DELETE from emailing where ID=$emailID AND (recipient = '$name') AND (folder = 'inbox')))";
}
mysql_db_query($DBName, $Query2, $Link);

?>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
nothing to worry inq123, i gave it up on this one :)

Author

Commented:
still can't submit the form... after I clicked the submit button.. nothing happens.. cannot post at all..


I put the code before or after the while loop also can't ..hmm whats wrong..
missing a </form> tag?  or a end ?> tag?

I am not sure if these were your full files so here it is

email.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
 
$Query = "SELECT * from emailing where (((recipient = '$name') AND (folder = 'inbox'))) ORDER BY ID DESC LIMIT 25";
$Result = mysql_db_query($DBName, $Query, $Link);

while($emailrow = mysql_fetch_array($Result)){

print "<form name='del' action='deleteemail.php' method='post'>";

print "<INPUT type='submit' name='delete' value='delete'>";

print "<table><tr>";

print "<td><input type='checkbox' name='deleterow[]' value=\"$emailrow[ID]\"></td>";

print "<td>$emailrow[sender]</td>";

print "<td>$emailrow[subject]</td>";

print "<td>$emailrow[date]</td>";

print "</table></tr>";
 
}

print "<INPUT type='submit' value='delete'>";
print "</FORM>"

?>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



deleteemail.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
foreach($_REQUEST['deleterow'] as $emailID) {
$Query2 = "DELETE from emailing where ID=$emailID AND (recipient = '$name') AND (folder = 'inbox')))";
}
mysql_db_query($DBName, $Query2, $Link);

?>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
put the form tag outside the while loop:

print "<form name='del' action='deleteemail.php' method='post'><table>;

while($emailrow = mysql_fetch_array($Result))
{
    print "<INPUT type='submit' name="delete" value='delete'>";
    print "<tr>";
    print "<td><input type='checkbox' name='deleterow[]' value=\"$emailrow[ID]\"></td>";
    print "<td>$emailrow[sender]</td>";
    print "<td>$emailrow[subject]</td>";
    print "<td>$emailrow[date]</td>";
    print "</tr>";
}
print "</table><INPUT type='submit' value='delete'>";

*****************************************************
& don't  use sender and subject in the query string:

 $Query2 = "DELETE from emailing where ID=$emailID";

if wes u'ld need to do the same as for $emailID (but u don't need that as $emailID is primary key)
quick fix to this file to add the '' around the emailid in the sql.

deleteemail.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
foreach($_REQUEST['deleterow'] as $emailID) {
$Query2 = "DELETE from emailing where ID='$emailID' AND (recipient = '$name') AND (folder = 'inbox')))";
}
mysql_db_query($DBName, $Query2, $Link);

?>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Author

Commented:
****...


my domain is down at this time!! sorry guys..Will test it as soon as it is up.
djerratik -
  good call i did not catch that one :)

Author

Commented:
Site up..

Solved the submit form error... sorry for my stupidness there a </form> tag in between my code..


The problem now is I can't delete any of the rows selected


for this line:

foreach($_REQUEST['deleterow'] as $emailID) {

$emailID is referring to $email[ID}] right?


--------------------------------------------------------------

djerratik

why don;t use sender and subject in the query string?

that are the fields of my database

Commented:
My suggestion (see my first post) is the same as djerratik's solution except that I suggested that you use one sql statement to delete all, for efficiency.  Using a loop to delete is not optimal at all.

So in my script the delete statement would be like:

$sql = "delete from mytable where ID in (" . implode(",", $delete) . ")";

Author

Commented:
inq123

can you submit the full code, I am quite confuse now
don't use sender and subject in the query string as u must do the same "array stuff" than for the ID. and u don't need them anyway as the ID as primary key is sufficient to delete the correct line in the table.

to inq123: u r completely right with the efficiency! :)

Commented:
your ID is auto id already so should be unique, then why have sender and subject in the string?

Commented:
Gee, you guys are fast.  Are you on this page refreshing constantly or what? :-)

Author

Commented:
inq123

the sender and subject is just from displaying only, remember I am using this for a custom webmail..

Author

Commented:
Back to the code.... why I can;t delete any rows.??


inq123 can you post the full code as combine to mine?

i let u both guys alone! hope u manage to finish this quickly...
i hope this will work for you.

email.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
 
$Query = "SELECT * from emailing where (((recipient = '$name') AND (folder = 'inbox'))) ORDER BY ID DESC LIMIT 25";
$Result = mysql_db_query($DBName, $Query, $Link);

print "<form name='del' action='deleteemail.php' method='post'>;

while($emailrow = mysql_fetch_array($Result)){

print "<INPUT type='submit' name="delete" value='delete'>";

print "<table><tr>";

print "<td><input type='checkbox' name='deleterow[]' value=\"$emailrow[ID]\"></td>";

print "<td>$emailrow[sender]</td>";

print "<td>$emailrow[subject]</td>";

print "<td>$emailrow[date]</td>";

print "</table></tr>";
 
}

print "<INPUT type='submit' value='delete'>";
print "</form>"

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



deleteemail.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
include ("webmail.php")  ;

$name="testing";
if (isset($_REQUEST['del'])) {
  foreach($_REQUEST['deleterow'] as $emailID) {
  $Query2 = "DELETE from emailing where ID='$emailID' AND (recipient = '$name') AND (folder = 'inbox')))";
  }
}
mysql_db_query($DBName, $Query2, $Link);

?>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Commented:
I was suggesting this for deleteemail.php:

<?
include ("webmail.php")  ;

$Query2 = "DELETE from emailing where ID in (" . implode(",", $deleterow) . ")";
echo $Query2; # I'm adding this just to make sure sql statement is right, you can use this and run on mysql command          
                        # line and see if it works
print_r($deleterow); # same purpose, debugging
mysql_db_query($DBName, $Query2, $Link);

?>

For the first php script, mine wouldn't differ from djerratik's.  My only suggestion as to why your delete did not work would be that maybe you're using old php before $_REQUEST is added to php.  What's your php version? just echo phpversion()

Some other unlikely places would be to check if your DB connection is swamped out, check your ID was output correctly on the html page the first php rendered and make sure first php is the same as djerratik's, etc.

Commented:
sorry, replace $deleterow in my code above with $_REQUEST['deleterow']
just for the sake of annoying u guys, but still, this won't work (if more than 1 line to b deleted) due to the missing/unclear recipient and folder variables! if u really want to show detailed deletion confirm, then pass the recipient & folder variables same as for the emailID. to b sure u may add the emailID to the variables (eg. )

(now i see that u don't even pass these variables in the form...)

...now i'm going home, it's 18:00 o'clock

Author

Commented:
Finally it works!

inq123,

implode works. thanks!


mwillbanks,

I don;t know why it can't works.. but the amending did a great help to me!

djerratik,

thanks for the input also !




Thank you very much guys!

Have a nice day


Author

Commented:
djerratik,

the deleting is based on the unique ID, so without the 2 variable it still works.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial