O'Reilly Forums: Chapter 4, Where Is The Error In My Code? - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 4, Where Is The Error In My Code?

#1 User is offline   Gacnt 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 23-January 13

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>

This post has been edited by Gacnt: 23 January 2013 - 04:38 PM

0

#2 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 608
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 24 January 2013 - 05:48 AM

View PostGacnt, on 23 January 2013 - 04:35 PM, said:

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?

This post has been edited by drewdin: 24 January 2013 - 06:31 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users