sford999 Posted August 10, 2009 Share Posted August 10, 2009 Hi I have the following code to delete messages from a table using checkboxes. <?php if(isset($_POST['submit'])) { $list = ""; $counter = 0; foreach($_POST['delete'] as $value) { $list+="'$value$'"; if($counter>0) $list+=","; $counter++; } $sql = "DELETE FROM messages WHERE id IN ($list)"; $result = mysql_query($sql) or die(sql_error(mysql_error(), $sql)); if(mysql_affected_rows() >= 1) { redirect('messages.php', 0); } else { echo mysql_error(); } } ?> The html thats producing the checkboxes <input name="delete[]" type="checkbox" id="delete[]" value="'.$id.'"> However when printing the mysql query its saying this DELETE FROM messages WHERE id IN (0) Doing a print_r() on $_POST['delete'] says this Array ( [0] => 33 [1] => 28 [2] => 27 [3] => 26 [4] => 25 ) However as you can see from the query, the array is not being passed to the query and I can't see where I have gone wrong. Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/169690-solved-php-update-loop-problems/ Share on other sites More sharing options...
wildteen88 Posted August 10, 2009 Share Posted August 10, 2009 Change this foreach($_POST['delete'] as $value) { $list+="'$value$'"; if($counter>0) $list+=","; $counter++; } To just $list = implode(', ', $_POST['delete']); Link to comment https://forums.phpfreaks.com/topic/169690-solved-php-update-loop-problems/#findComment-895169 Share on other sites More sharing options...
taquitosensei Posted August 10, 2009 Share Posted August 10, 2009 use implode $sql = "DELETE FROM messages WHERE id IN (".implode(",",$_POST['delete']).")"; to do it your way it would have to be $list.=$value; // not plus Link to comment https://forums.phpfreaks.com/topic/169690-solved-php-update-loop-problems/#findComment-895170 Share on other sites More sharing options...
sford999 Posted August 10, 2009 Author Share Posted August 10, 2009 Just tried your suggestions and I'm having this error: Warning: implode() [function.implode]: Invalid arguments passed in C:\server\htdocs\date\messages.php on line 119 Print_r($_POST['delete']); Array ( [0] => 33 [1] => 28 [2] => 27 [3] => 26 ) Link to comment https://forums.phpfreaks.com/topic/169690-solved-php-update-loop-problems/#findComment-895179 Share on other sites More sharing options...
sford999 Posted August 11, 2009 Author Share Posted August 11, 2009 NM I fixed it, thanks Guys Link to comment https://forums.phpfreaks.com/topic/169690-solved-php-update-loop-problems/#findComment-895209 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.