我不是最擅长 php 编码,只是学习它。但我想知道为什么我不断将变量传递给 each() 不是数组或对象警告。我知道有一个类似的线程我曾用于此代码,但该帖子没有解决,所以我希望它可能是错误是它指出的第 46 行 -
while(list($key,$val) = each($_POST['checkbox'])) {
整个代码是
<?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>
请帮我解决这个问题。