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

Link to comment
Share on other sites

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

?>

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.