0

我目前正在尝试将 xml 标题的值发布到一个数组中,该数组又将用于保存假期。我仍然没有收到任何错误消息,并且使用了 var_dump 来收集以下信息。(勾选了几个与阅读 RSS 提要相关的复选框)

array(7) { [0]=> string(21) "{$currentItem->title}" [1]=> string(21)"{$currentItem->title}" [2]=> string(21) "{$currentItem->title}" [3]=> string(21) "{$currentItem->title}" [4]=> string(21) "{$currentItem->title}" [5]=> string(21) "{$currentItem->title}" [6]=> string(21) "{$currentItem->title}" } :S 

这对我来说表明数组端正在工作,但它没有保存在复选框的值参数中设置的信息,因为所有字符串都是 21?21是saveBox的值的引号之间的字符数!!

index.php 中的部分

$index = 1;
    foreach ($allHolidays as $currentItem)
        {
            echo '<tr>';
            if (isset($_SESSION['login']))
                {
                    echo '<td valign="top">';
                    //echo '<input type="hidden" name="guid$index" value="{$currentItem->guid}">';name="saveBox$index[]"
                    echo '<input type="checkbox" name="saveBox[]" value="{$currentItem->title}">';
                    echo '</td>';
                }
            echo '<td>';
            echo "<p><a href=\"{$currentItem->link}\">{$currentItem->title}</a><br/>";
            echo "{$currentItem->description}<br/>";
            echo "{$currentItem->pubDate}<br/></p>";
            echo '</td>';
            echo '</tr>';
            $index++;
        }

保存进程.php

<?php   
    header("refresh:555; url='index.php'");
    session_start();

    echo "Thank you for saving a holiday";
    echo '<input type="checkbox" name="saveBox[]" value="'.
    htmlspecialchars($currentItem->title).'">';
    include "top.php";
    var_dump($_POST['saveBox']);

    try
    {

        foreach ($_POST['saveBox'] as $savedHoliday)
        {
            $user = $_SESSION['login'];
            $currentSave = $savedHoliday;
            $save = "channel/item[title=\"$currentSave\"]";
            $holidaysXML = simplexml_load_file('holidays.xml');
            $savePath = $holidaysXML->xpath($save);
            foreach($savePath as $currentSavePath)
                    {
                        echo "<p><a href='{$currentSavePath->link}'>{$currentSavePath->title}</a>"."<br\>".
                        "{$currentSavePath->description}"."<br\>".
                        "{$currentSavePath->pubDate}"."<br\></p>";

                        $insertSave = $db->prepare("INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`)
                        VALUES ('$user', '$currentSavePath->link', '$currentSavePath->pubDate', '$currentSavePath->title', '$currentSavePath->description')");

                        $insertSave->execute();
                    }

        }
    }
    catch(PDOException $e)
        {
            echo $e->getMessage();
        }
4

1 回答 1

1

您使用的构造saveBox$index[]错误。您应该简单地命名您的输入saveBox[]并忘记索引。$_POST['saveBox']当您在接收端读取时,PHP 会自动为您提供一个数组。

不要忘记您还应该调用htmlspecialchars嵌入到 HTML 中的所有数据:

echo '<input type="checkbox" name="saveBox[]" value="'.
     htmlspecialchars($currentItem->title).'">';

和:

foreach ($_POST['saveBox'] as $savedHoliday)

顺便说一句,您应该始终在产生任何输出之前调用headerand session_start,因此如果top.php产生输出,它应该向下移动几行。

于 2012-05-02T10:54:25.673 回答