Jump to content

delet multiple rows


corillo181

Recommended Posts

alright i'm working on a messeges system ad i'm wondering how do i make it so when the people check the boxes every box selected allt he value in mysql is deleted..

[x]-msg1
[]-msg2
[x]-msg3
[x]-msg4
[]-msg5
[x]-msg6

[delete]

in the other end what would be my query to make the x msgs to be deleted?
Link to comment
Share on other sites

If you make each checkbox pass the id of the record to delete, your php might look something like...

[code]
<?php
  // connect
  foreach($_POST as $id) {
    mysql_query("DELETE FROM tbl WHERE id = '$id'");
  }
?>
[/code]

Of course you'll need to impliment error checking and the like, but that'll give you the idea.[/code]
Link to comment
Share on other sites

this is something i came up with but i dont know how to pass the checkbox value to the $_post for the each statement
[code]
$querypms=mysql_query("SELECT * FROM mypms")or die(mysql_error());
?><form action="<?php $_SERVER['PHP_SELF']?>" method="post" >
<?php
while($echopm=mysql_fetch_array($querypms)){
?>
<input name="<?php echo $echopm['id'];?>" type="checkbox" value="<?php echo $echopm['id'];?>" /><?php echo $echopm['subject'];?><br />

<?php
}
?>
<input name="Submit" type="submit" id="Submit" value="Delete" />
</form>
<?php
if($_POST['Submit']){
foreach($_POST as $del){
$del=mysql_query("DELETE * FROM mypms WHERE id='$id'");

if($del){
echo "done";
}
}
}
?>
[/code]
Link to comment
Share on other sites

[code]
<?php
  if ($_POST['Submit']) {
    foreach($_POST as $id) {
      if ($id != 'Delete') {
        if (mysql_query("DELETE * FROM mypms WHERE id='$id'")) {
          echo "done<br />";
        }
      }
    }
  }
?>
[/code]
Link to comment
Share on other sites

[code]
<?php
include_once 'db.php';
$querypms=mysql_query("SELECT * FROM mypms")or die(mysql_error());
?><form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" >
<?php
while($echopm=mysql_fetch_array($querypms)){
?>
<input name="<?php echo $echopm['id'];?>" type="checkbox" value="<?php echo $echopm['id'];?>" /><?php echo $echopm['subject'];?><br />

<?php
}
?>
<input name="Submit" type="submit" id="Submit" value="Delete" />
</form>
<?php
  if ($_POST['Submit']) {
    foreach($_POST as $id) {
      if ($id != 'Delete') {
        if (mysql_query("DELETE * FROM mypms WHERE id='$id'")) {
          echo "done<br />";
        }
      }
    }
  }
  ?>
[/code]


it does nothing at all..
Link to comment
Share on other sites

All those methods can use an enormous number of queries! Remeber you are probably limited to 50 queries per script run - so if someone selects 51 you aint gonna get teh results you wanted.

if you declare each check box like so..
[code]<?php
while($echopm=mysql_fetch_array($querypms)){
?>
<label>
<input name="delete[]" type="checkbox" value="<?php echo $echopm['id'];?>" />
Delete <?php echo $echopm['subject'];?></label><br />
<?php
}
?>[/code]

Then to delete all the mesages where the user has checked the corresponding box simply do this..

[code]<?php
$ids = implode(',',$_POST['delete']);

$qry = "DELETE FROM `mypms` WHERE `id` IN(" . $ids . ")";
$qry = mysql_query($qry);
?>[/code]

Beatuy of that little lot is there far les code - no loops - AND it will delete how ever many posts are selected - not limited to the number of queries you can execute.
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.