Jump to content

Foreach not working


dual_alliance

Recommended Posts

Hello,

 

I have a list of checkbox's each represents an event id, l'm trying to make it so when the user selects that checkbox then clicks the form, the checkbox's selected will be deleted.

 

<input mutiple type='checkbox' name='eid[]' value='<?php echo $row1['e_id']; ?>'>

 

and for the processing

 

if($_GET['act'] == "delsel"){

$eid = Clean($_POST['eid']);
$query = mysql_query("SELECT `e_id` from `events` WHERE `e_for` = '$memberid'");
$row = mysql_fetch_array($query);
$eid2 = $row['e_id'];
foreach ($eid as $eid2)
{
$query4 = "DELETE FROM `events` WHERE `e_id` = '$eid'";
$result4 = mysql_query($query4) or die("Query Failed");
}
echo '<br /><br /><center>Event\'s Succesfully Deleted<br /><br />
<a href="events.php">Back</a></center>';	
}

 

The error l get..

Warning: Invalid argument supplied for foreach() in ******** on line 117

 

I know it doesnt look right, but l've never used foreach before and dont get how it works.  Your help/insight is greatly appreciated ;D

Link to comment
https://forums.phpfreaks.com/topic/46272-foreach-not-working/
Share on other sites

Wow...seriously that makes no sense what so ever.

 

if($_GET['act'] == "delsel"){

$eid = Clean($_POST['eid']);
if (!is_array($eid)) {
   die('This is not an array!');
}
$query = mysql_query("SELECT `e_id` from `events` WHERE `e_for` = '$memberid'");
$row = mysql_fetch_array($query);
$eid2 = $row['e_id']; // you set this here but is over-written in the foreach
foreach ($eid as $eid2)
{
$query4 = "DELETE FROM `events` WHERE `e_id` = '$eid'"; // you are setting the value to be "array" because $eid should be an array?
$result4 = mysql_query($query4) or die("Query Failed");
}
echo '<br /><br /><center>Event\'s Succesfully Deleted<br /><br />
<a href="events.php">Back</a></center>';

}

 

Note the comments, figure out what you are doing first than re-post it.

Link to comment
https://forums.phpfreaks.com/topic/46272-foreach-not-working/#findComment-225072
Share on other sites

LOL :D ok.  Tried to make it clear, l have no idea when it comes to using foreach()

 

// Do they want to delete the selected?
if($_GET['act'] == "delsel"){

// Clean the posted variable
$eid = Clean($_POST['eid']);
if (!is_array($eid)) {
   die('This is not an array!');
}
// Not sure if this is really nessasary for the foreach command, but get the event id from where it is for the member
$query = mysql_query("SELECT `e_id` from `events` WHERE `e_for` = '$memberid'");
$row = mysql_fetch_array($query);
// Not sure about this bit either, but make the gotten results into the variable $eid2
$eid2 = $row['e_id']; // you set this here but is over-written in the foreach
foreach ($eid as $eid2)
{
// Delete the row where the eid = $eid
$query4 = "DELETE FROM `events` WHERE `e_id` = '$eid'"; 
// you are setting the value to be "array" because $eid should be an array?

$result4 = mysql_query($query4) or die("Query Failed");
}
// Tell the user what has happened
echo '<br /><br /><center>Event\'s Succesfully Deleted<br /><br />
<a href="events.php">Back</a></center>';

}

Link to comment
https://forums.phpfreaks.com/topic/46272-foreach-not-working/#findComment-225081
Share on other sites

Foreach ($array as $key => $value)

 

for every item in $array assign the key of that index to $key and the value of the index to $value and loop through until every element of the array has been touched.

 

foreach ($array as $value)

for every item in $array assign the value of the index to $value and loop through until every element of the array has been touched.

 

Pretty basic as long as you understand arrays.

 

<?php
// Do they want to delete the selected?
if($_GET['act'] == "delsel"){

// Clean the posted variable
$eid = Clean($_POST['eid']);
if (!is_array($eid)) {
   die('This is not an array!');
}
// Not sure if this is really nessasary for the foreach command, but get the event id from where it is for the member
$query = mysql_query("SELECT `e_id` from `events` WHERE `e_for` = '$memberid'");
$row = mysql_fetch_array($query);
// Not sure about this bit either, but make the gotten results into the variable $eid2
$eid2 = $row['e_id']; // you set this here but is over-written in the foreach
foreach ($eid as $eid2)
{
// Delete the row where the eid = $eid
$query4 = "DELETE FROM `events` WHERE `e_id` = '$eid2'";  // changed to $eid2 since that is the value you seemingly want,
// you are setting the value to be "array" because $eid should be an array?

$result4 = mysql_query($query4) or die("Query Failed");
}
// Tell the user what has happened
echo '<br /><br /><center>Event\'s Succesfully Deleted<br /><br />
<a href="events.php">Back</a></center>';

}
?>

Link to comment
https://forums.phpfreaks.com/topic/46272-foreach-not-working/#findComment-225083
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.