Jump to content


Photo

Chapter 4, Where Is The Error In My Code?


  • Please log in to reply
1 reply to this topic

#1 Gacnt

Gacnt

    New Member

  • Members
  • Pip
  • 1 posts

Posted 23 January 2013 - 04:35 PM

Getting error on line 14.
Warning: Invalid argument supplied for foreach() in /home/sithhapp/public_html/Elivs/removeemail.php on line 14

<html>
<head>
<title></title>
</head>
<body>


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php
	$dbc = mysqli_connect(*Connection works fine, removed for this post*) or die('Error connecting to MySQL server.');
	
	if (isset($_POST['submit'])) {
Line 14 >	foreach ($_POST['todelete'] as $delete_id) {
			$query = "DELETE FROM email_list WHERE id = $delete_id";
			mysqli_query($dbc, $query) or die('Error querying database');
		}
		echo 'Customer(s) removed.<br />';
	}
	
	$query = "SELECT * FROM email_list";
	$result = mysqli_query($dbc, $query);
	while ($row = mysqli_fetch_array($result)) {
		echo '<input type="checkbox" value="' . $row['id'] . '"  name="todelete[]" />';
		echo $row['first_name'];
		echo ' ' . $row['last_name'];
		echo ' ' . $row['email'];
		echo '<br />';
	}
	
	mysqli_close($dbc);
?>

<input type="submit" name="submit" value="Remove" />
</form>

</body>
</html>

Edited by Gacnt, 23 January 2013 - 04:38 PM.


#2 drewdin

drewdin

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 608 posts
  • Gender:Male
  • Location:Boston

Posted 24 January 2013 - 05:48 AM

Getting error on line 14.

Warning: Invalid argument supplied for foreach() in /home/sithhapp/public_html/Elivs/removeemail.php on line 14

<html>
<head>
<title></title>
</head>
<body>


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php
	$dbc = mysqli_connect(*Connection works fine, removed for this post*) or die('Error connecting to MySQL server.');
	
	if (isset($_POST['submit'])) {
Line 14 >	foreach ($_POST['todelete'] as $delete_id) {
			$query = "DELETE FROM email_list WHERE id = $delete_id";
			mysqli_query($dbc, $query) or die('Error querying database');
		}
		echo 'Customer(s) removed.<br />';
	}
	
	$query = "SELECT * FROM email_list";
	$result = mysqli_query($dbc, $query);
	while ($row = mysqli_fetch_array($result)) {
		echo '<input type="checkbox" value="' . $row['id'] . '"  name="todelete[]" />';
		echo $row['first_name'];
		echo ' ' . $row['last_name'];
		echo ' ' . $row['email'];
		echo '<br />';
	}
	
	mysqli_close($dbc);
?>

<input type="submit" name="submit" value="Remove" />
</form>

</body>
</html>



I ran your code and it works fine, the only issue is that it does not check and see if the array is empty. so if you don't check a box and hit remove an empty array will be sent and cause the error you list.

So when do you get the error, are you selecting values and getting this error?

Edited by drewdin, 24 January 2013 - 06:31 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users