Dysan Posted December 27, 2007 Share Posted December 27, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83408-delete-selectedchecked/ Share on other sites More sharing options...
lemmin Posted December 27, 2007 Share Posted December 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83408-delete-selectedchecked/#findComment-424356 Share on other sites More sharing options...
SirChick Posted December 27, 2007 Share Posted December 27, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83408-delete-selectedchecked/#findComment-424358 Share on other sites More sharing options...
lachild Posted December 27, 2007 Share Posted December 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83408-delete-selectedchecked/#findComment-424359 Share on other sites More sharing options...
Barand Posted December 28, 2007 Share Posted December 28, 2007 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)" ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83408-delete-selectedchecked/#findComment-424390 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.