0

I am having multi record delete problem. It shows an error about foreach loop.

  <?php if(isset($_REQUEST['action'])&&$_REQUEST['action']=="delete") 
          {

             $value=$_REQUEST['check'];
            foreach($value as $ids)
            {

  $dquery="SELECT songname FROM songs WHERE id='$ids'";
  $dresult=mysql_query($dquery);
  $drow=mysql_fetch_array($dresult);
  $songtodel=$drow['songname'];
  $path2="../upload/$songtodel";
  if(is_file("../upload/$songtodel"))
  unlink($path2);
  $mdquery="DELETE FROM songs WHERE id='$ids'"; 
  mysql_query($mdquery); 

            }

          } 

Edited by mike_2000_17: Fixed formatting

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by cossay
0

What kind of error does it show? I think you should also post the code for your form.

0

QWarning: Invalid argument supplied for foreach() in D:\wamp\www\greenmusic\admin\sec.php on line 409

0

<td width="4%"><input type="checkbox" name="master" onClick="checkedAll('adminsform')" value=""/></td>

0

The argument you pass to the foreach should be an array. A question, do you have checkboxes in your form called check? If you have, then, instead of

<input type = "checkbox" name = "check"/>

you should write

<input type = "checkbox" name = "check[]"/>

Edited by cossay: n/a

0

Here is an example of what I mean, assuming your form is below

<form method = "post" action = "your_server_script.php">
	<input type = "checkbox" name = "check[]" value = "A"/>
	<input type = "checkbox" name = "check[]" value = "B"/>
	<input type = "checkbox" name = "check[]" value = "C"/>
	<input type = "checkbox" name = "check[]" value = "D"/>
	<input type = "checkbox" name = "check[]" value = "E"/>
	<input type = "submit" name = "but" value = "DELETE"/>
</form>

now you can write your foreach like this

<?php
	if(isset($_POST['but']))//If the user has clicked the submit button
	{
		$check = $_POST['check'];
		
		foreach($check as $ck)
		{
			//Your query here
		}
	}

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