2012-05-02 41 views
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++; 
     } 

saveProcess.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(); 
     } 
+2

我已經讀了你的'問題'三次,不能讓它的東西... – stUrb

+0

我試圖檢查我的數組,以確保正確的信息被複選框,因爲我不能得到它存儲在數據庫中。原來,複選框是持有正確的信息,但仍然不能將這些信息存儲到我的數據庫,我不知道哪裏出了錯誤謝謝 –

回答

1

節您使用的是結構saveBox$index[]錯誤。您應該簡單地將您的輸入命名爲saveBox[],並忘記索引。當您在接收端閱讀$_POST['saveBox']時,PHP將自動爲您提供一個數組。

不要忘了,你也應該呼籲htmlspecialchars上的所有數據,你嵌入到HTML:

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

和:

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

順便說一句,你應該總是電話headersession_start生成任何輸出之前,所以如果top.php產生輸出它應該向下移動幾行。

+0

現在看看我儘快回覆你 –

+0

iv將我的代碼更改爲saveBox和var拋出陣列,它似乎是保持正確的信息不知道爲什麼它沒有正確處理,因爲它沒有給我錯誤信息 –

+0

var_dump顯示該數組正在通過21個字符和21個字符是確切的字符數保存框值字段之間。你有什麼想法爲什麼它保存字符串而不是XML路徑?謝謝 –