我收到此错误消息,我不知道如何处理它。
警告:在第31行的/home/a9083956/public_html/zundappgroesbeek/beheer/testretrievesql.html中为foreach()提供的参数无效
这是我正在使用的代码。警告来自第31行,foreach()
行。我已将=
更改为=>
以使复选框正常工作,但删除代码仍无效。
<html>
<head>
<title>Retrieve and delete data from database </title>
</head>
<body>
<?php
// Connect to database server
mysql_connect("mysql7.000webhost.com", "a9083956_test", "sesam") or die (mysql_error ());
// Select database
mysql_select_db("a9083956_test") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM forum_question";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
echo '<input name="delete['.$row['id'].']" type="checkbox">';
echo $row['topic']. " " .$row['name']. " " .$row['datetime'] . "<br />";
$delete = $_POST['delete'];
foreach($delete as $id => $value)
{
$id = mysql_real_escape_string($id);
mysql_query("DELETE FROM table_name WHERE id => $id");
}
}
// Close the database connection
mysql_close();
?>
</body>
</html>
答案 0 :(得分:1)
试试这个:
while($row = mysql_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
echo '<input name="delete[]" type="checkbox">';
echo $row['topic']. " " .$row['name']. " " .$row['datetime'] . "<br />";
}
$delete = mysql_real_escape_string($_POST['delete']);
for($i=0; $i < count($delete); $i++){
mysql_query("DELETE FROM table_name WHERE id = $delete[$i] ");
}
答案 1 :(得分:1)
如果您没有选中任何框,则不会设置$_POST['delete']
。在if (isset($_POST['delete']))
之前使用foreach
。
你的SQL中还有一个错误,你想要WHERE id = "$id"
,否则它会删除比你想要的更多的行......