Vidya_tr Posted April 20, 2009 Share Posted April 20, 2009 I have a page that displays the name of some files wih a checkbox against it.The list is created and displayed dynamically when each time a record is added to the database.There is a DELETE button also. The files has to be selected using checkbox and when the delete button is clicked the selected records should be deleted from the database and its name should be cleared from the list. The post action of delete button calls delete_file.php where the code for the purpose has to be written. How can I write the code to identify the checked checkboxes and do the action? The code for the page creating the list is given below.. <form name="f1" method="post" action="delete_file.php"> <input name="delete" type="button" value="Delete video" /> <div align="left" > <?php $sql="SELECT * FROM video_links"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { echo "<br>"; ?> <input type="checkbox" name="chk[]" id="delcheckbox" value="<?php echo $row['id']; ?>"/> <a href="#"><?php echo $row['title'] ?></a> <?php } echo "<br>"; ?> </div> </form> googled a lot. but not able to find a proper solution .Please help.... Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/ Share on other sites More sharing options...
GingerRobot Posted April 20, 2009 Share Posted April 20, 2009 See this tutorial i wrote on the subject. Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814291 Share on other sites More sharing options...
Vidya_tr Posted April 20, 2009 Author Share Posted April 20, 2009 thank you... I was able to make the concept clear through your tutorial. But for me,still some problem exits. My code to delete the records is as follows.. <?php include('config.php'); if(count($_POST) > 0) { $delcheck=$_POST['deletechk']; $delcheck= implode(",", $delcheck); //echo $delcheck; mysql_query("DELETE from video_links where link_id IN($delcheck)") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("DELETE from video where videoID IN($delcheck)") or trigger_error(mysql_error(),E_USER_ERROR); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <input name="delete" type="button" value="Delete Video" /> <div align="left" > <?php $sql="SELECT * FROM video_links"; $result = mysql_query($sql); if($result==NULL) {echo "<br>No videos uploaded"; exit();} while($row = mysql_fetch_array($result)) { echo "<br>"; echo '<input type="checkbox" name="deletechk[]" value="'.$row['link_id'].' " /> <a href="#"> '.$row['videoTitle'].'</a>'; } //echo "</table>"; echo "<br>"; ?> </div> </form> </body> </html> But with this code it is not even identifying whether the DELETE button is clicked or not.. The $_POST of DELETE button is not working... Please help me to find where I went wrong in this code... Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814307 Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 You've used deletechk[] for the name of the checkboxes and this places those ticked into an array called deletechk so you can run a foreach() loop on it like this: foreach $(deletechk as $chk) { echo 'User selected '.$chk.'<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814313 Share on other sites More sharing options...
Vidya_tr Posted April 20, 2009 Author Share Posted April 20, 2009 My problem is that, the if loop of the code if(count($_POST) > 0) {... } is not working.. I am not able to find what goes wrong when the delete button is clicked... action POST is not taking place even after selecting the checkbox and clicking the delete button Please help me... :'( Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814318 Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 Detect the delete button with this: if (isset($_POST['delete'])) { //something as delete button was clicked } Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814322 Share on other sites More sharing options...
Vidya_tr Posted April 20, 2009 Author Share Posted April 20, 2009 After changing the code to if(isset($_POST['delete'])) {....} it is not working. Will there be any problem in $_SERVER['PHP_SELF'] ? Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814341 Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 Try this, all I've changed is: * Changed your button to a SUBMIT button * Indented the code properly * Changed the check to isset() instead of using count <?php include('config.php'); if (isset($_POST['subdelete']) { $delcheck=$_POST['deletechk']; $delcheck= implode(",", $delcheck); //echo $delcheck; mysql_query("DELETE from video_links where link_id IN($delcheck)") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("DELETE from video where videoID IN($delcheck)") or trigger_error(mysql_error(),E_USER_ERROR); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <input name="subdel" type="submit" value="Delete Video" /> <div align="left" > <?php $sql="SELECT * FROM video_links"; $result = mysql_query($sql); if($result==NULL) { echo "<br>No videos uploaded"; exit(); } while($row = mysql_fetch_array($result)) { echo "<br>"; echo '<input type="checkbox" name="deletechk[]" value="'.$row['link_id'].' " /> <a href="#"> '.$row['videoTitle'].'</a>'; } //echo "</table>"; echo "<br>"; ?> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814344 Share on other sites More sharing options...
Vidya_tr Posted April 20, 2009 Author Share Posted April 20, 2009 Thank you so much... I got it solved.... The problem was that the button was not of type=submit. I changed it to type submit and used isset($_POST[]) It works fine.. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814352 Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 * Changed your button to a SUBMIT button Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814355 Share on other sites More sharing options...
Vidya_tr Posted April 20, 2009 Author Share Posted April 20, 2009 One more small doubt...... I am not able to delete more than one record at a time It shows the follwing warning and error... Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\DEVTPN\videolist.php on line 22 Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 in C:\wamp\www\DEVTPN\videolist.php on line 30 Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814366 Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 if (isset($_POST['subdelete']) { foreach ($_POST['deletechk'] as $d) { mysql_query("DELETE FROM video_links WHERE link_id=".$d." LIMIT 1") or trigger_error(mysql_error(),E_USER_ERROR); } } That will loop through the selected items deleting them one by one. Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814369 Share on other sites More sharing options...
Vidya_tr Posted April 20, 2009 Author Share Posted April 20, 2009 YES.... It worked!!! Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814379 Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 Glad to hear! Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814384 Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 Only thing I'd recommend is to prevent against MySQL injection. If your ID field is a numerical value (which I suspect it is) then use this: if (isset($_POST['subdelete']) { foreach ($_POST['deletechk'] as $d) { mysql_query("DELETE FROM video_links WHERE link_id=".intval($d)." LIMIT 1") or trigger_error(mysql_error(),E_USER_ERROR); } } That will return 0 if anything other than a number is sent. You can perform other checks but that's a start. Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814386 Share on other sites More sharing options...
GingerRobot Posted April 20, 2009 Share Posted April 20, 2009 Only thing I'd recommend is to prevent against MySQL injection. If your ID field is a numerical value (which I suspect it is) then use this: if (isset($_POST['subdelete']) { foreach ($_POST['deletechk'] as $d) { mysql_query("DELETE FROM video_links WHERE link_id=".intval($d)." LIMIT 1") or trigger_error(mysql_error(),E_USER_ERROR); } } That will return 0 if anything other than a number is sent. You can perform other checks but that's a start. One of the points of my tutorial was to avoid making multiple queries for something like this. You only need the one if you use an IN clause. You can validate using the array_map function prior to executing the query. Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814408 Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 GingerRobot - I for one will be reading your tutorial as I've not thought of using IN like this before! Quote Link to comment https://forums.phpfreaks.com/topic/154831-solved-deleting-the-checked-records/#findComment-814414 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.