jackgoddy123 Posted March 19, 2014 Share Posted March 19, 2014 Hello, I am stuck in one of my application module. I have to set multiple delete option in my application. I have used the below code. The designing is perfectly fine. All what i want to add multiple delete options in it. Below is the code which i have tried out: <html> <head> <title>Strad Hosting Limited -Web Hosting</title> <script language="javascript"> function validate() { var chks = document.getElementsByName('checkbox[]'); var hasChecked = false; for (var i = 0; i < chks.length; i++) { if (chks[i].checked) { hasChecked = true; break; } } if (hasChecked == false) { alert("Please select at least one."); return false; } return true; } </script> </head> <body > <?php $con=mysqli_connect("localhost","stradsol","D#v,b5TnQ!D!","stradsol_sales"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM Persons"); echo "<form name='form1' method='post' action='' onSubmit='return validate();'>"; echo "<table border='1' style=' background-color: white;'> <tr> <th></th> <th>id</th> <th style='padding-left: 11px;'>Lead Generated Date </th> <th>name</th> <th>email</th> <th>contacts</th> <th>requirement</th> <th style='display:none;'>Company name</th> <th style='display:none;'>Address</th> <th>Next Follow-Up date</th> <th>Final_Details</th> <th style='display:none;'>Status</th> <th style='padding-left: 12px; display:none;'>Lead Closed Date</th> <th>EDIT</th> <th>Follow-up History</th> <th>Add Follow-up</th> <th>Delete Record</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['date'] . "</td>"; echo "<td><a href='config_info.php?id=" . $row['id'] . "'>".$row['name']."</a></td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['contacts'] . "</td>"; echo "<td>" . $row['requirement'] . "</td>"; echo "<td style='display:none;'>" . $row['company_name'] . "</td>"; echo "<td style='display:none;'>" . $row['address'] . "</td>"; echo "<td>" . $row['startdate'] . "</td>"; echo "<td>" . $row['final_details'] . "</td>"; echo "<td style='display:none;'>" . $row['status'] . "</td>"; echo "<td style='display:none;'>" . $row['lead_close'] . "</td>"; echo "<td><a href='edit_eg1.php?id=" . $row['id'] . "'>Edit</a></td>"; echo "<td><a href='Follow_history.php?id=" . $row['id'] . "'>Follow_history</a></td>"; echo "<td><a href='add_follow.php?id=" . $row['id'] . "'>Followup</a></td>"; echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>"; echo "</tr>"; } echo "<tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr>"; $count=mysqli_num_rows($result); if(isset($_POST['delete'])){ for($i=0;$i<count($_POST['checkbox']);$i++){ $del_id=$_POST['checkbox'][$i]; $sql = "DELETE FROM Persons WHERE id='$del_id'"; echo $sql; $result2 = mysqli_query($con,$sql); } // if successful redirect to delete_multiple.php if($result2) { echo "<meta http-equiv=\"refresh\" content=\"0;URL=edit_table_del.php\">"; } } mysqli_close($con); echo "</table>"; echo "</form>"; ?> <br><br> <a href="index2.php">[ Back To Home ]</a> </body> </html> I am stuck, the query is not working i thing. Dont know what i am missing. It will be greatful is some one can help me out with this. Quote Link to comment https://forums.phpfreaks.com/topic/287087-multiple-delete-through-checkout/ Share on other sites More sharing options...
Ch0cu3r Posted March 19, 2014 Share Posted March 19, 2014 (edited) $rows['id'] should be $row['id'] Using queries within loops is not recommended. You can delete multiple records from on query by using an IN clause $ids = array_map('intval', $_POST['checkboxes']); $sql = 'DELETE FROM Persons WHERE id IN('. implode(',' $ids) .')'; $result = mysql_query($sql); Also you should be processing the form deletion before constructing the page <?php $con=mysqli_connect("localhost","*****","*****","stradsol_sales"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // process the form first if(isset($_POST['delete'])) { $ids = array_map('intval', $_POST['checkboxes']); $sql = 'DELETE FROM Persons WHERE id IN('. implode(',' $ids) .')'; $result = mysqli_query($con, $sql); // if successful redirect if($result) { header('Location: edit_table_del.php'); exit; // kill the page when performing a redirect } } // no construct the page ?> <html> <head> <title>Strad Hosting Limited -Web Hosting</title> <script language="javascript"> function validate() { var chks = document.getElementsByName('checkbox[]'); var hasChecked = false; for (var i = 0; i < chks.length; i++) { if (chks[i].checked) { hasChecked = true; break; } } if (hasChecked == false) { alert("Please select at least one."); return false; } return true; } </script> </head> <body > <?php $result = mysqli_query($con,"SELECT * FROM Persons"); echo "<form name='form1' method='post' action='' onSubmit='return validate();'>"; echo "<table border='1' style=' background-color: white;'> <tr> <th></th> <th>id</th> <th style='padding-left: 11px;'>Lead Generated Date </th> <th>name</th> <th>email</th> <th>contacts</th> <th>requirement</th> <th style='display:none;'>Company name</th> <th style='display:none;'>Address</th> <th>Next Follow-Up date</th> <th>Final_Details</th> <th style='display:none;'>Status</th> <th style='padding-left: 12px; display:none;'>Lead Closed Date</th> <th>EDIT</th> <th>Follow-up History</th> <th>Add Follow-up</th> <th>Delete Record</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['date'] . "</td>"; echo "<td><a href='config_info.php?id=" . $row['id'] . "'>".$row['name']."</a></td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['contacts'] . "</td>"; echo "<td>" . $row['requirement'] . "</td>"; echo "<td style='display:none;'>" . $row['company_name'] . "</td>"; echo "<td style='display:none;'>" . $row['address'] . "</td>"; echo "<td>" . $row['startdate'] . "</td>"; echo "<td>" . $row['final_details'] . "</td>"; echo "<td style='display:none;'>" . $row['status'] . "</td>"; echo "<td style='display:none;'>" . $row['lead_close'] . "</td>"; echo "<td><a href='edit_eg1.php?id=" . $row['id'] . "'>Edit</a></td>"; echo "<td><a href='Follow_history.php?id=" . $row['id'] . "'>Follow_history</a></td>"; echo "<td><a href='add_follow.php?id=" . $row['id'] . "'>Followup</a></td>"; echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>"; echo "</tr>"; } echo "<tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr>"; mysqli_close($con); echo "</table>"; echo "</form>"; ?> <br><br> <a href="index2.php">[ Back To Home ]</a> </body> </html> Edited March 19, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/287087-multiple-delete-through-checkout/#findComment-1473141 Share on other sites More sharing options...
Psycho Posted March 19, 2014 Share Posted March 19, 2014 (edited) Well, you have an individual delete link for each record. You want to change that to have a form and include a checkbox for each record with a single button to delete the selected records. Your code is very hard to follow, but the logic appears to be backwards. It appears you are outputting the page THEN processing the deletes. It needs to be the other way around, otherwise you will be displaying records that were meant to be deleted THEN deleting them. Another issue, element IDs in the HTML page MUST be unique. You gave all the checkboxes the same IDs. Also, the JavaScript function has some unnecessary code. Once you've determined that at least one record is checked - just return true. Lastly, there is no need to have different processing to delete a single record vs. deleting multiple records. You just need to call the same page and format the input data appropriately. I made a change for the single and multiple deletes to call back to this same page, make the delete and then refresh the list. You can change that, but you should definitely have both delete But, here's the biggest problem while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>"; The variable you are assigning the query result to is $row, but you are referencing $rows Here's a complete rewrite. There could be some minor typos to resolve, but the logic is much more solid <?php $con=mysqli_connect("localhost","stradsol","D#v,b5TnQ!D!","stradsol_sales"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //See if any records were selected to be deleted if(isset($_POST['ids'])) { //Force all values to be integers are remove 0 values $deleteIDsList = array_filter(array_map('intval', $_POST['ids'])); //Create and run ONE delete query $query = "DELETE FROM Persons WHERE id IN ($deleteIDsList)"; $result = mysqli_query($con, $query); } //Get list of current records to display $query = "SELECT * FROM Persons"; $result = mysqli_query($con, $query); $output = ''; while($row = mysqli_fetch_array($result)) { $output .= "<tr>\n"; $output .= " <td><input name='ids[]' type='checkbox' id='' value='{$row['id']}'></td>\n"; $output .= " <td>{$row['id']}</td>\n"; $output .= " <td>{$row['date']}</td>\n"; $output .= " <td><a href='config_info.php?id={$row['id']}'>{$row['name']}</a></td>\n"; $output .= " <td>{$row['email']}</td>\n"; $output .= " <td>{$row['contacts']}</td>\n"; $output .= " <td>{$row['requirement']}</td>"; $output .= " <td style='display:none;'>{$row['company_name']}</td>\n"; $output .= " <td style='display:none;'>{$row['address']}</td>\n"; $output .= " <td>{$row['startdate']}</td>\n"; $output .= " <td>{$row['final_details']}</td>\n"; $output .= " <td style='display:none;'>{$row['status']}</td>\n"; $output .= " <td style='display:none;'>{$row['lead_close']}</td>\n"; $output .= " <td><a href='edit_eg1.php?id={$row['id']}'>Edit</a></td>\n"; $output .= " <td><a href='Follow_history.php?id={$row['id']}'>Follow_history</a></td>\n"; $output .= " <td><a href='add_follow.php?id={$row['id']}'>Followup</a></td>\n"; $output .= " <td><a href='?ids[]={$row['id']}'>Delete</a></td>\n"; $output .= "</tr>\n"; } mysqli_close($con); ?> <html> <head> <title>Strad Hosting Limited -Web Hosting</title> <script language="javascript"> function validate() { var chks = document.getElementsByName('ids[]'); for (var i=0; i<chks.length; i++) { if (chks[i].checked) { return true; } } //No records were checked alert("Please select at least one."); return false; } </script> </head> <body> <form name='form1' method='post' action='' onSubmit='return validate();'> <table border='1' style='background-color: white;'> <tr> <th></th> <th>id</th> <th style='padding-left: 11px;'>Lead Generated Date </th> <th>name</th> <th>email</th> <th>contacts</th> <th>requirement</th> <th style='display:none;'>Company name</th> <th style='display:none;'>Address</th> <th>Next Follow-Up date</th> <th>Final_Details</th> <th style='display:none;'>Status</th> <th style='padding-left: 12px; display:none;'>Lead Closed Date</th> <th>EDIT</th> <th>Follow-up History</th> <th>Add Follow-up</th> <th>Delete Record</th> </tr> <?php echo $output; ?> <tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr> </table> </form> <br><br> <a href="index2.php">[ Back To Home ]</a> </body> </html> Edited March 19, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/287087-multiple-delete-through-checkout/#findComment-1473157 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.