Jump to content

Display All Records with Checkbox


Dysan

Recommended Posts

Hi.

 

I have the following code, that displays records in my database.

How do I display each record along with a checkbox, then when a delete button is clicked, delete the selected records?

 

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM person");

while($row = mysql_fetch_array($result))
{
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
}

Link to comment
https://forums.phpfreaks.com/topic/76677-display-all-records-with-checkbox/
Share on other sites

<?php

if (isset($_POST['submit'])){
   foreach ($_POST['delete'] as $id){
      $query = "DELETE FROM person WHERE personID='$id'";
      $result = mysql_query($query)or die(mysql_error());
   }
   
   echo "Records Deleted<p>";
}
   

$result = mysql_query("SELECT * FROM person");

echo "<form action='{$_SERVER['PHP_SELF']}' method='post'>";
while($row = mysql_fetch_array($result)){
  echo "<input type='submit' name='delete[]' value='{$row['personID']}'> ";
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
}
echo '<input type="submit" name="submit" value="Delete Records">';
echo '</form>';

?>

 

Wherever you see the row "personID" is where you need to replace that with whatever your unique ID is for that table.

So you mean do this?

 

<?php

if (isset($_POST['submit'])){
   foreach ($_POST['delete'] as $id){
      $query = "DELETE FROM person WHERE personID=(".implode(",",$_POST['delete']).")";
      $result = mysql_query($query)or die(mysql_error());
   }
   
   echo "Records Deleted<p>";
}
   

$result = mysql_query("SELECT * FROM person");

echo "<form action='{$_SERVER['PHP_SELF']}' method='post'>";
while($row = mysql_fetch_array($result)){
  echo "<input type='submit' name='delete[]' value='{$row['personID']}'> ";
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
}
echo '<input type="submit" name="submit" value="Delete Records">';
echo '</form>';

?>

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.