ozone1 Posted December 3, 2009 Share Posted December 3, 2009 Hi guys i need a little help i want to delete multiple rows from mysql with checkbox. Here is my code everything is listing but delete function is not working please have a look at my code and tell me where i went wrong i am a noob in php. please thanks <?php require('../config.php'); include('header.php'); ?> <table width="800" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action="report.php"> <table width="800" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF">ID</td> <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Comment</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>IP</strong></td> </tr> <?php $result = mysql_query("SELECT * FROM report ORDER BY id"); while($row = mysql_fetch_array($result)) { $id = $row['id']; $email = $row['email']; $link = $row['link']; $comment = $row['comment']; $ip = $row['ip']; ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['id']; ?>"></td> <td bgcolor="#FFFFFF"><? echo $row['email']; ?></td> <td bgcolor="#FFFFFF"><? echo $row['link']; ?></td> <td bgcolor="#FFFFFF"><? echo $row['comment']; ?></td> <td bgcolor="#FFFFFF"><? echo $row['ip']; ?></td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <? // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM report WHERE id='$del_id'"; $result = mysql_query($sql); } // if successful redirect to report.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=report.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table> in config.php i have this <?php $user = 'my_user'; $pass = 'my_pass'; $host = 'localhost'; $db = 'my_db'; @mysql_connect($host,$user,$pass) or die(mysql_error()); @mysql_select_db($db) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/183860-deleting-multiple-rows-with-checkbox-help-please/ Share on other sites More sharing options...
rajivgonsalves Posted December 3, 2009 Share Posted December 3, 2009 your $delete should be $_POST['delete'] and so on for the other variables unless your register_globals are on or you have extracted the variables. Quote Link to comment https://forums.phpfreaks.com/topic/183860-deleting-multiple-rows-with-checkbox-help-please/#findComment-970572 Share on other sites More sharing options...
ozone1 Posted December 3, 2009 Author Share Posted December 3, 2009 thanks rajiv for responding i dnt know much about php it will be really helpful if you can correct the code for me please Quote Link to comment https://forums.phpfreaks.com/topic/183860-deleting-multiple-rows-with-checkbox-help-please/#findComment-970578 Share on other sites More sharing options...
rajivgonsalves Posted December 3, 2009 Share Posted December 3, 2009 I think you should try some trial error thats how you will learn however just take the following code as one off help hopefully it will work, hopefully I assume you are trying this on a test server. // Check if delete button active, start this if($_POST['delete']){ $sql = "DELETE FROM report WHERE id IN (" . implode(',', $_POST['checkbox'] . ")"; $result = mysql_query($sql); } Quote Link to comment https://forums.phpfreaks.com/topic/183860-deleting-multiple-rows-with-checkbox-help-please/#findComment-970582 Share on other sites More sharing options...
mrMarcus Posted December 3, 2009 Share Posted December 3, 2009 try this: <?php if (isset ($_POST['delete'])) { if (is_array ($_POST['checkbox'])) { $del_ids = implode ("','", mysql_real_escape_string ($_POST['checkbox'])); $sql = "DELETE FROM `report` WHERE `id` IN ('{$del_ids}')"; $result = mysql_query ($sql) or trigger_error (mysql_error()); } } ?> in place of: <?php // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM report WHERE id='$del_id'"; $result = mysql_query($sql); } ?> do you have register_globals on? Quote Link to comment https://forums.phpfreaks.com/topic/183860-deleting-multiple-rows-with-checkbox-help-please/#findComment-970584 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.