I'm not the best at php coding, just learning it. But i was wondering why I keep getting a variable passed to each() is not an array or object warning. I know there is a similar thread that I had used for this code but that post was not resolved so I am hoping it can be the error is one line 46 where it states -
while(list($key,$val) = each($_POST['checkbox'])) {
the whole code is
<?php
$host = 'localhost'; // Host name
$username = 'root'; // Mysql username
$password = ''; // Mysql password
$db_name = 'forms'; // Database name
$tbl_name = 'members'; // Table name
// Connect to server and select databse.
mysql_connect($host, $username, $password) or die('cannot connect');
mysql_select_db($db_name) or die('cannot select DB');
$sql = 'SELECT * FROM `'.$tbl_name.'`';
$result = mysql_query($sql);?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
</tr>
<?php while ($rows = mysql_fetch_array($result)): ?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['id']; ?>]" type="checkbox" id="checkbox[<? echo $rows['id']; ?>]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['Member ID']; ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['firstname']); ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['lastname']); ?></td>
</tr>
<?php endwhile; ?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if ($_POST['delete']) {
$i = 0;
while(list($key,$val) = each($_POST['checkbox'])) {
$sql = "DELETE FROM $tbl_name WHERE id= '$val'";
mysql_query($sql);
$i += mysql_affected_rows();
}
if($i > 0){
echo '<meta http-equiv="refresh" content="0;URL=delete_member3.php">';
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
Please help me resolve this.
First of all, you don't have a form element named "checkbox", so $_POST['checkbox'] is going to be null (it should be throwing an undefined index error). Second of all, a html checkbox is going to return a boolean (true/false) value when the form is being processed.
Also, I don't think you understand the usage of each(). It returns an array containing the key and value of the current array element, and then advance the array pointer. So in an array, if you have:
$a = array('foo' => 'bar', 'dog' => 'cat');
$b = each($a);
var_dump($b);
$b now contains an array like this:
array( 0 => 'foo', 'key' => 'foo', 1 => 'bar', 'value' => 'bar' )
And next time you use each() on $a, it will return similar results using the dog/cat key/value pair.
Insert a hidden field with the same name before the checkbox:
PHP Code:
<input type='hidden' name='x' value='0'>
<input type='checkbox' name='x' value='1'>
So your script receive either "x=0" or "x=1".
Another Way
it's better to check for NULL. try to implement the following:
PHP Code:
<?php
foreach($_POST as $key => $value)
{
echo ($value !== NULL) $key . ' = ' . $value . '<br />' : '';
}
?>