Jump to content

Recommended Posts

The following PHP code, displays records contained inside a database, along with a check box for each one.

 

Upon a check box and delete button being clicked, how do I delete the selected records?

 

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
  die(mysql_error());
}

mysql_select_db("db", $con);

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

echo '<table border="1">
<tr>
<th><input type="checkbox" name="checkbox" value="checkbox"></th>
<th>First Name</th>
<th>Last Name</th>
</tr>';

while($row = mysql_fetch_array($result))
{
  echo '<tr>';
  echo '<td><input type="checkbox" name="checkbox" value="checkbox"></td>';
  echo '<td>'.$row['FirstName'].'</td>';
  echo '<td>'.$row['LastName'].'</td>';
  echo '</tr>';
}
echo '</table>';

mysql_close($con);
?>

Link to comment
https://forums.phpfreaks.com/topic/83408-delete-selectedchecked/
Share on other sites

The $_POST array will contain a key of "checkbox" (the name of a checkbox) with a value of "on" for each checkbox that is checked and it won't have anything if it isn't checked. So...

if (isset($_POST['checkbox']))
   //this checkbox was checked.

 

I think he meant how do you match the check box to the row that the check box corresponds to so that the script will deleted the correct row.. right?

Do you have an id or key setup for your records?  If you do then you can change the checkbox name to give you something to work with... Like this

 

while($row = mysql_fetch_array($result))
{
  echo '<tr>';
  echo '<td><input type="checkbox" name="delete['.$row['id'].']" value="Y"></td>';
  echo '<td>'.$row['FirstName'].'</td>';
  echo '<td>'.$row['LastName'].'</td>';
  echo '</tr>';
}

 

This when posted would generate an array like

array(
     delete => array (
          1 => Y,
          6 => Y
     )
)

 

Then in the script you handle the post info you can do something like the following

<?php

foreach ($_POST['delete'] as $key => $value){
     if ($value == 'Y')
          $sql = 'DELETE FROM `person` WHERE `id` LIKE '.$key.';';
     else
          continue;
     }
?>

 

hope that helps

On right lines but a better solution is to make the record id the value of the c/box

<?php
echo "<td><input type='checkbox' name='delete[]' value='{$row['id']}'></td>";

 

Then instead of looping and having a series of individual delete queries you only need a single query

 

<?php
$del = join (',', $_POST['delete']);

mysql_query ("DELETE FROM person WHERE id IN ($del)" );
?>

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.