未定义的索引和无效的foreach()的参数的PHP(undefined index and inv

2019-10-16 15:52发布

我有一个网页,其中显示哪些用户已保存到MySQL数据库的项目和回声它与AA复选框一起。 当用户点击该复选框,并单击保存按钮,该记录将被删除,但我遇到了我的标题错误,我想。

节假日显示形式。

        <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)行,我不能明白为什么它不工作,任何帮助,将不胜感激。

注意:未定义指数:德尔/var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11026991/public_html/Ass/deleteProcess.php在线14

警告:在/var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11026991/public_html/Ass/deleteProcess.php为的foreach()供给线14上的参数无效

Answer 1:

del复选框未从形式张贴在所有。 您需要添加method="post"到表单中。

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



Answer 2:

你应该先检查是否$ _POST [“删除”]设置,因为如果没有复选框被选中也不会。

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


Answer 3:

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



Answer 4:

method="post"是从缺失<form action

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

删除的语法是:

DELETE FROM table_name WHERE condition


Answer 5:

你缺少method="post"从您的窗体所以没有被公布,这是你的问题。



Answer 6:

这个错误是相当清楚的。

$_POST['del']

不存在,因为del是不是一个有效字段到发布的形式。

如果del是复选框的名字,你必须首先通过添加以检索他们post action ,以您的形式



文章来源: undefined index and invalid foreach() argument php