【问题标题】:undefined index and invalid foreach() argument php未定义的索引和无效的foreach()参数php
【发布时间】:2012-05-07 01:40:39
【问题描述】:

我有一个页面显示用户已保存到 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 上尝试过 var_dump 吗?有数据吗?看起来,$_POST 的 del 索引没有设置,这会导致 foreach 无效。

标签: php forms undefined-index


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    &lt;form action="deleteProcess.php" method="post"&gt;

    【讨论】:

      【解决方案3】:

      您通过 $_POST 访问您的复选框值,但您的 Form-Tag 没有定义任何方法! 你必须设置&lt;form method="POST"&gt;,否则你必须使用$_GET['del']

      【讨论】:

        【解决方案4】:

        &lt;form action 中缺少method="post"

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

        删除语法是:

        DELETE FROM table_name WHERE condition
        

        【讨论】:

          【解决方案5】:

          这个错误很明显。

          $_POST['del']
          

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

          如果del 是复选框的名称,您首先需要通过在表单中​​添加post action 来检索它们

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              猜你喜欢
              • 2014-05-15
              • 2011-04-28
              • 2023-03-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-04-14
              • 1970-01-01
              • 2016-04-04
              相关资源
              最近更新 更多