Dysan Posted December 28, 2007 Share Posted December 28, 2007 How do I delete a record, upon the user clicking the check box displayed next to each record, and a delete button being clicked? <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die(mysql_error()); } mysql_select_db("db", $con); $result = mysql_query("SELECT * FROM person"); while($row = mysql_fetch_array($result)) { echo '<input type="checkbox" name="checkbox" value="checkbox">'; echo $row['FirstName']; echo $row['LastName'].'<br /><br />'; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/ Share on other sites More sharing options...
p2grace Posted December 28, 2007 Share Posted December 28, 2007 have the checkbox id contain the id of the person in the database. Then when the form is submitted create a loop that goes through each checkbox and deletes the record associated. Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424933 Share on other sites More sharing options...
Dysan Posted December 28, 2007 Author Share Posted December 28, 2007 OK, I'm confused. Can we take this slowly? How do I use the "FirstName" field as the unique identifier, instead of id? I have come up with the following code. What do I need to do next? <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die(mysql_error()); } mysql_select_db("db", $con); $result = mysql_query("SELECT * FROM person"); while($row = mysql_fetch_array($result)) { echo '<input type="checkbox" name="checkbox[]" value="'.$row['FirstName'].'">'; echo $row['FirstName']; echo $row['LastName'].'<br /><br />'; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424949 Share on other sites More sharing options...
p2grace Posted December 28, 2007 Share Posted December 28, 2007 Here's how I'd do it: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die(mysql_error()); } mysql_select_db("db", $con); if(isset($_POST['checkbox'])){ $delete_arr = $_POST['checkbox']; foreach($delete_arr as $value){ $query = "DELETE FROM `db` WHERE `id` = '$value'"; $run = mysql_query($query); } echo "Items deleted successfully."; } $result = mysql_query("SELECT * FROM person"); echo '<form name="deleteForm" id="deleteForm" action="filename.php" method="post" enctype="multipart/form-data">'; while($row = mysql_fetch_assoc($result)){ extract($row); echo '<input type="checkbox" name="checkbox[]" value="'.$id.'">'; echo $FirstName; echo $LastName.'<br /><br />'; } echo '<input type="submit" />'; echo '</form>'; mysql_close($con); ?> I haven't tested this but it should work. Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424951 Share on other sites More sharing options...
Dysan Posted December 28, 2007 Author Share Posted December 28, 2007 What's the name of the file, the code is in? How would you do it using FirstName as the unique identifier? Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424957 Share on other sites More sharing options...
p2grace Posted December 29, 2007 Share Posted December 29, 2007 You'd replace $id with $FirstName, but I'd highly recommend not using the First Name as the unique identifier. There could be a lot of table rows with the first names are the same. Every table you make in a database should have an id associated to it. I'd highly recommend using the row id verse any other field where data could be repeated. Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424959 Share on other sites More sharing options...
Dysan Posted December 29, 2007 Author Share Posted December 29, 2007 For what I'm building the application for, there can only be one instance of a name. Upon the user entering the names/data, the database will be searched, to ensure that the database doesn't already contain an instance of the selected name. In other words, the name field, is similar to the username within an email address (the bit before the @) once it's taken, it can't be used again. Is it possible to split the code up into to files. Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424974 Share on other sites More sharing options...
p2grace Posted December 29, 2007 Share Posted December 29, 2007 I see, sounds like you have it figured out Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424975 Share on other sites More sharing options...
Dysan Posted December 29, 2007 Author Share Posted December 29, 2007 Upon the user selecting records to delete, and after the "Delete" button has been clicked, I want to display a div using JavaScript, containing delete confirm buttons "Yes" and "No". Upon the "Yes" button being clicked, how do I delete the selected records. Also, how do I create a separate file/page that displays a "Yes" and "No" confirm buttons, and navigate to it if JavaScript was disable on the previous page? Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424987 Share on other sites More sharing options...
p2grace Posted December 29, 2007 Share Posted December 29, 2007 Instead of using js to display a div, perhaps a better idea would be to have it display a confirm box: <script type="text/javascript"> var confirmed = confirm("Are you sure you wish to delete this person?"); if(confirmed == true){ // Run script to post the form data } </script> Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424989 Share on other sites More sharing options...
Dysan Posted December 29, 2007 Author Share Posted December 29, 2007 Good idea, but how do I navigate to a confirm page that deletes the records using PHP, if JavaScript is turned off? e.g. Upon clicking "Yes", the delete code on page is activated. Upon clicking "No", the user is returned to where he/she was before. <html> <body> <form name="form1" method="post" action=""> Are you sure you want to delete?<br> <br> <input type="submit" name="Submit" value="Yes"> <input type="submit" name="Submit2" value="No"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/83521-delete-selected-record/#findComment-424991 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.