<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.

Recommended Answers

All 2 Replies

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);

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() ) ;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.