Jump to content

DELETE MULTIPLE ROWS | PHP/MYSQL


logicopinion

Recommended Posts

Hello,

 

with the code below i delete 1 row even if several rows are selected, what should i do to be able to delete as much rows as much i select at once.

 

<?php
mysql_connect("localhost", "root", "") or die ("Could not connect");
mysql_select_db("db1") or die ("Could not connect to DB");
if ($_POST['delete'])

{

$deleteID = $_POST['delete'];

mysql_query("DELETE FROM dbtable WHERE id='$deleteID'") or die(mysql_error());

echo "The Row Number $deleteID has been Successfully Removed";

  
    }
else 

{ 

echo "please select at least one row, to delete it";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/72597-delete-multiple-rows-phpmysql/
Share on other sites

Firstly, you would need to make your form use arrays, then you simply implode on that array to form your query. eg;

 

<form method="post">
  <input type="checkbox" name="del[]" value="1">
  <input type="checkbox" name="del[]" value="2">
  <input type="checkbox" name="del[]" value="3">
  <input type="submit" name="submit">
</form>
<?php

  if (isset($_POST['submit'])) {
    $sql = "DELETE FROM dbtable WHERE id IN('" . implode("','",$_POST['del']) . "');";
    if (mysql_query($sql)) {
      echo "The Rows Number " . implode(" ",$_POST['del']) . " where Successfully Removed";
    } else { 
      echo "please select at least one row, to delete it";
    }
  }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.