提问人:Darren Burgess 提问时间:4/26/2012 最后编辑:pb2qDarren Burgess 更新时间:5/22/2012 访问量:740
未定义的索引和无效的 foreach() 参数 php
undefined index and invalid foreach() argument php
问:
我有一个页面,其中显示了用户已保存到MYSQL数据库的项目,并将其与复选框一起回显出来。我希望当用户单击此复选框并单击保存按钮时,记录将被删除,但我在标题中遇到了错误。
显示假期表单。
<form action="deleteProcess.php">
<?php
foreach($db->query($sql) as $row)
{
echo "<p>" . $row['title'] . "<br \>" .
$row['description'] . "<br \>" .
$row['link'] . "<br \>" .
$row['pubDate'] .
"<input type='checkbox' name='del[]' value='$row/['title'/]'/>" .
"</p>";
}
?>
<input type="submit" name="delete" value="submit" />
</form>
删除进程 .php
foreach($_POST['del'] as $check_value)
{
//takes session value and assigns it to a variable so that when a holiday is saved it can be distinguished by user
$user = $_SESSION['name'];
//assign the value of previous checkbox to $check
$check = $check_value;
//assign xpath query to variable $fav
$fav = "channel/item [title = \"$check\"]";
//loads instance of xml file as $holidayDoc
$holidayDoc = simplexml_load_file('holidays.xml');
//executes xpath search within holidays.xml and results stored in $favourites
$favourites = $holidayDoc->xpath($fav);
//for each element of the associative array $favourites, key will be referred to as $currentFav.
foreach($favourites as $currentFav)
{
echo "{$currentFav->link}". "<br \>";
echo "{$currentFav->title}". "<br \>";
echo "{$currentFav->description}". "<br \>";
echo "{$currentFav->pubDate} ". "<br \>";
//sql statement that states which values will be inserted into which column
$sql = "DELETE FROM `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`)
VALUES ('$user', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";
//connect to database then execute the sql statement.
$db->exec($sql);
//close connection to the database
$db = null;
错误显示在行上,我不明白为什么它不起作用,任何帮助将不胜感激。foreach($_POST['del'] as $check_value)
注意:未定义索引:del in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11026991/public_html/Ass/deleteProcess.php 上线 14
警告:为 foreach() 提供的参数无效 /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11026991/public_html/Ass/deleteProcess.php 上线 14
答:
您应该首先检查是否设置了 $_POST['del'],因为如果没有选中任何复选框,则不会设置。
if(isset($_POST['del'])){
foreach($_POST['del'] as $del){}
}
您的复选框根本没有从表单中发布。您需要添加到表单中。del
method="post"
<form action="deleteProcess.php" method="post">
您可以通过 $_POST 访问复选框值,但您的 Form-Tag 没有定义方法!
你必须设置 ,否则你必须使用<form method="POST">
$_GET['del']
method="post"
中缺少 。<form action
此外,用于删除的 SQL 语法不正确。从数据库中删除记录时,无需指定列。删除整个记录。
删除语法为:
DELETE FROM table_name WHERE condition
这个错误非常清楚。
$_POST['del']
不存在,因为不是已发布表单中的有效字段。del
如果是复选框的名称,则首先必须通过添加到表单中来检索它们del
post action
您的表格中缺少您,因此没有发布任何内容,这是您的问题。method="post"
评论