angelali Posted May 20, 2012 Share Posted May 20, 2012 I want to delete rows in a table of my database using check-box. Here are my codes below: <?php $con = mysql_connect('localhost', 'root', '') or die ('Connection Failed'); mysql_select_db('img', $con) or die ('Connection Failed'); $display = mysql_query("SELECT * FROM photos WHERE email='$lemail'"); echo '<input type="submit" value="Delete" name="del"/>'; echo "<table> <tr> <th>#</th> <th>Images</th> <th>Image description</th> <th>Delete</th> </tr>"; while($row = mysql_fetch_array($display)) { echo "<tr>"; echo "<td>".$row['img_ID']."</td>"; echo "<td><img src='folder/".$row['imaged']."' alt='alt text' width='100' height='100' class='thumb'/> </td>"; echo "<td>".$row['image_description']."</td>"; echo '<td><input type="checkbox" name="delete[]" value="'.$row['img_ID'].'"/></td>'; echo "</tr>"; } echo "</table>"; echo "</form>"; if (isset($_POST['delete'])) { $del = $row['img_ID']; for($i=0;$i<count($_POST["delete"]);$i++) { if($_POST["delete"][$i] != "") { $str = "DELETE FROM photos WHERE img_ID='$del' "; mysql_query($str); echo "Record Deleted."; } } } mysql_close($connect); ?> Here are the problems: 1/ Not working at all, I mean no image is being deleted 2/ At then end, I display a message that the record has been deleted, but if I check multiple checkbox, it keeps writing the message "Records deleted" multiple times My images are stored in a folder while its details in database... Help, thank you Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/ Share on other sites More sharing options...
angelali Posted May 20, 2012 Author Share Posted May 20, 2012 UPDATE: Now it is working, but only the message keep displaying multiple times! HELP! Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347100 Share on other sites More sharing options...
doddsey_65 Posted May 20, 2012 Share Posted May 20, 2012 The message will display several times because it is within a loop. So if you delete 5 images, the message will be displayed 5 times. Rather than executing the query within a loop (a very bad idea) you should use something like: if (isset($_POST['delete'])) { foreach($_POST['delete'] as $id => $val) { $ids[] = $id; } mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")"); echo "Record Deleted."; } This assumes the checkboxes have the name "delete[$row['img_ID']]". The foreach loop will loop through them all and add the img_ID to an array($ids) The query will then delete all images where the img_ID exists in the array($ids). When they query is finished it will echo your message only once. also this wont work: $del = $row['img_ID']; its outside the variable scope of the loop it is used in ($row). Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347108 Share on other sites More sharing options...
angelali Posted May 20, 2012 Author Share Posted May 20, 2012 Still not deleting, but it does give me the message it has been deleted. Is something wrong in the codes? <?php $con = mysql_connect('localhost', 'root', '') or die ('Connection Failed'); mysql_select_db('img', $con) or die ('Connection Failed'); $display = mysql_query("SELECT * FROM photos WHERE email='$lemail'"); echo '<input type="submit" value="Delete" name="del"/>'; echo "<table> <tr> <th>#</th> <th>Images</th> <th>Image description</th> <th>Delete</th> </tr>"; while($row = mysql_fetch_array($display)) { echo "<tr>"; echo "<td>".$row['img_ID']."</td>"; echo "<td><img src='folder/".$row['imaged']."' alt='alt text' width='100' height='100' class='thumb'/> </td>"; echo "<td>".$row['image_description']."</td>"; echo '<td><input type="checkbox" name="delete[]" value="'.$row['img_ID'].'"/></td>'; echo "</tr>"; } echo "</table>"; echo "</form>"; if (isset($_POST['delete'])) { foreach($_POST['delete'] as $id => $val) { $ids[] = $id; } mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")"); echo "Record Deleted."; } mysql_close($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347110 Share on other sites More sharing options...
angelali Posted May 20, 2012 Author Share Posted May 20, 2012 Help! :'( Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347118 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 Shouldn't the line in the foreach loop be: $ids[] = $val; If it still doesn't work, output the query string: echo "DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")"; Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347126 Share on other sites More sharing options...
angelali Posted May 20, 2012 Author Share Posted May 20, 2012 Well, I successfully did it well...thank you guys.. however to problems remain: 1/ How can I tell the user that the checkbox must be checked if he clicked on the delete button without checking? Because, i used POST here, and I think 'empty()' or ' !="" ' will not work! I tried '!isset' also. 2/ I tried implementing mysql_real_escape_string and strip_tags to the checkbox, but gives me an error as it is conflicting with the FOR Each loop Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347127 Share on other sites More sharing options...
Jessica Posted May 20, 2012 Share Posted May 20, 2012 print out the $_POST array using print_r, and see what is submitted when you don't check a box. Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347129 Share on other sites More sharing options...
angelali Posted May 20, 2012 Author Share Posted May 20, 2012 I did print_r($_POST); just after the code of echo "Records deleted"; when I click the button Delete without checking a checkbox, nothing appears.. Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347132 Share on other sites More sharing options...
Jessica Posted May 20, 2012 Share Posted May 20, 2012 You should at least see Array() and your submit. Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347138 Share on other sites More sharing options...
angelali Posted May 20, 2012 Author Share Posted May 20, 2012 No, nothing appears Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347142 Share on other sites More sharing options...
Jessica Posted May 20, 2012 Share Posted May 20, 2012 Turn on error reporting. Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347144 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 I think this would work? if (isset($_POST['delete'])) { // mysql delete code here... } else { echo "Select a checkbox!"; } Print_r doesn't show anything because your condition is testing for the existence of $_POST['delete']. If it doesn't exist, print_r isn't called at all! Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347145 Share on other sites More sharing options...
Pikachu2000 Posted May 20, 2012 Share Posted May 20, 2012 When you're trying to debug by printing the $_POST/$_GET/$_REQUEST array, it makes no sense at all to have the print_r() in a conditional. Put it as the first thing after the opening <?php tag. Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347146 Share on other sites More sharing options...
angelali Posted May 20, 2012 Author Share Posted May 20, 2012 Error reporting is always on when im on localhost.. And yes, Pikachu2000 is right, when I put it after <?php, it gives me Array () Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347148 Share on other sites More sharing options...
angelali Posted May 20, 2012 Author Share Posted May 20, 2012 Guys, sorry, I have to go, its very late here, if possible, just help me with these two remaining issues, willl reply you in some hours.. thank you Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347150 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 If $_POST is giving Array() when no checkbox is selected, simply check for the non-existence of $_POST['delete'] i.e. (!isset($_POST['delete'])) or use the if-else conditional posted above. Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347151 Share on other sites More sharing options...
Jessica Posted May 20, 2012 Share Posted May 20, 2012 So you can use if(!isset($_POST['name of checkbox'])){ } Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347152 Share on other sites More sharing options...
angelali Posted May 20, 2012 Author Share Posted May 20, 2012 Well, I tried that before, I even tries with !isset, but it writes the message which to display to the user that a checkbox should be checked before clicking on the delete button.. If this is not a bad principle or whatever, I will forget it, however, the mysql_real_escape_string and the strip_tags are more important.... I tried, but gives me an error.... Just tell me where to implement them thats all.. For example, I tried this: if (isset($_POST['delete'])) {....... $dimg = mysql_real_escape_string(strip_tags($_POST['delete'])); But it gave me an error, which I forgot as I already removed it... Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347153 Share on other sites More sharing options...
Jessica Posted May 20, 2012 Share Posted May 20, 2012 Well that's isset() not !isset() What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347154 Share on other sites More sharing options...
angelali Posted May 21, 2012 Author Share Posted May 21, 2012 Let's forget the isset for some minutes, what's more important is to protect the checkbox from attacks, I tried the following methods to protect the checkbox from attacks: if (isset($_POST['delete'])) { $del_img = mysql_real_escape_string(strip_tags($_POST['delete'])); foreach($del_img as $id => $val) { $ids[] = $val; } mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")"); echo "Record Deleted."; } I got these errors: Warning: strip_tags() expects parameter 1 to be string Warning: Invalid argument supplied for foreach() Notice: Undefined variable: ids Warning: implode() [function.implode]: Invalid arguments passed Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347215 Share on other sites More sharing options...
darkfreaks Posted May 21, 2012 Share Posted May 21, 2012 this should help solve most of those errors <?php $del_img = (string) $_POST['delete']; if (isset($del_img)) { $delete = mysql_real_escape_string(strip_tags($del_img)); $deleted = str_split($delete); foreach($deleted as $id) { $ids[] = implode(',',$id); } mysql_query("DELETE FROM photos WHERE img_ID IN ($ids)"); echo "Record Deleted."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347221 Share on other sites More sharing options...
angelali Posted May 21, 2012 Author Share Posted May 21, 2012 What you mean by (string) in the first line of code..? Quote Link to comment https://forums.phpfreaks.com/topic/262836-deleting-rows-in-mysql-databse-using-checkbox-not-working-help/#findComment-1347233 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.