未定义的索引和无效的foreach()参数php

时间:2012-04-26 10:17:34

标签: php forms undefined-index

我有一个页面,显示用户已保存到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>

删除process.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行

6 个答案:

答案 0 :(得分:2)

您的del复选框根本没有从表单中发布。您需要在表单中添加method="post"

<form action="deleteProcess.php" method="post">

答案 1 :(得分:1)

您应首先检查是否设置了$ _POST ['del'],因为如果没有选中复选框,则不会。

if(isset($_POST['del'])){
    foreach($_POST['del'] as $del){}
}

答案 2 :(得分:1)

您可以通过$ _POST访问您的复选框值,但您的Form-Tag没有定义方法! 您必须设置<form method="POST">,否则必须使用$_GET['del']

答案 3 :(得分:1)

method="post"中缺少

<form action

您的删除SQL语法也不正确。从数据库中删除记录时,不指定列。您删除整个记录。

删除语法为:

DELETE FROM table_name WHERE condition

答案 4 :(得分:1)

您的表单中缺少method="post",因此没有发布任何内容,这是您的问题。

答案 5 :(得分:0)

这个错误很清楚。

$_POST['del']

不存在,因为del不是发布表单中的有效字段。

如果del是复选框的名称,您首先要通过向表单添加post action来重新审核它们