0
<input type="checkbox"  name="opid[]"   id="<?php echo $row['opid'];?>"value="<?php echo $row['opid'];?>"value="<?php echo $_POST['opid'];?>" />
<label for = "<?php echo $row['opid'];?>"></label>
<?php
$opid= $_POST['opid'];
$opids ="";
foreach ($_POST['opid'] as $opid ) $opids.=((int)$opid)."," ;
$opids=substr($opids,0,-1); 
$sql="DELETE FROM opentopic WHERE opid IN ($opids)";

$res = mysql_query($sql) or die(mysql_error() ) ;
?>

Hi, I used the above code to delete multiple records with checkboxes. The code is working 100%. All checked information are deleted. THe problem is however that when I open my form I receive the following error message. Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\hello\delchange4.php on line 111 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.....This is at the line which says foreach ($_POST as $opid ) $opids.=((int)$opid)."," ;.
Once I have selected the checkboxes and pressed delete, the error message is gone. I would like to remove the error message. If anyone can please help. Maybe I am overlooking an obvious mistake. Thank you.

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by LethargicCoder
0

The only problem I see is that you have it concatenated but it doesn't look like it needs to be because you are not outputting anything to the browser. I also don't understand why the comma is there.

foreach ($_POST['opid'] as $opid ) $opids = ((int)$opid);
0

With the portion of code you provided, it looks to me that your problem is $_POST

When you first load your form, 'opid' doesn't exist in the _POST as you haven't posted it yet. The next error is then the result of an empty $opids making your SQL ... IN ()

Wrap an IF block around the following and only do if _POST exists
foreach ($_POST as $opid ) $opids.=((int)$opid)."," ;
$opids=substr($opids,0,-1);
$sql="DELETE FROM opentopic WHERE opid IN ($opids)";

$res = mysql_query($sql) or die(mysql_error() ) ;

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.