2012-04-26 23 views
1

我有一個頁面,顯示用戶已保存到MYSQL數據庫的項目,並將其與一個複選框一起回顯。我想當用戶點擊這個複選框,並點擊保存按鈕,該記錄將被刪除,但我遇到了我的標題中的錯誤。未定義的索引和無效的foreach()參數php

顯示假期表格。

 <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行

警告:在線路中用於 的foreach()供給/var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w11026991/public_html/Ass/deleteProcess.php 無效參數14

+0

你試過$ _POST上的var_dump了嗎?有沒有數據?看起來,$ _POST的del索引沒有設置,然後導致無效的foreach。 – Ahatius 2012-04-26 10:21:29

回答

2

del複選框未從形式張貼在所有。您需要將method="post"添加到您的表單中。

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

+0

不能相信我錯過了這個謝謝。 – 2012-04-26 10:23:22

1

您第一應檢查是否設置了$ _POST ['del'],因爲如果沒有複選框被選中,不要。

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

您通過$ _ POST訪問您的複選框的值,但你的表單的標籤定義沒有方法! 你必須設置<form method="POST">,否則,您必須使用$_GET['del']

1

method="post"<form action丟失。

另外,您的用於刪除的SQL語法不正確。從數據庫中刪除記錄時,您不指定列。您刪除整個記錄。

的刪除語法是:

DELETE FROM table_name WHERE condition 
0

此錯誤是相當清楚的。

$_POST['del'] 

不存在,因爲del不是發佈表單中的有效字段。

如果del是複選框的名字,你必須首先通過添加post action到窗體

1

您從形式缺少method="post"所以沒有被貼以檢索他們,這是你的問題。