2016-07-26 118 views
0

我有一些PHP顯示如下的HTML表單: enter image description here 然後在按下更新按鈕時更新表中的信息。帶複選框的HTML表單元素

我的問題是刪除選項。任何時候我打的更新按鈕,信息更新成功,但我得到了delete語句此錯誤消息: enter image description here

下面是代碼:// 信息連接到數據庫的心願$ =服務器名「.COM」; $ dbusername =「」; $ password =「」; $ dbname =「」;

try { 
     // To connect to the database please 
     $conn = new mysqli($servername, $dbusername, $password, $dbname); 
     if ($conn->connect_error) { 
      die('Connect Error (' . $conn->connect_errno . ') ' 
       . $conn->connect_error); 
     } 

     echo "Please click <strong><a href = 'http://eggcavity.com/add-wishlist'>here</a></strong> to add creatures to your wishlist."; 

     if(isset($_POST['submit'])){ 
      $ids = $_POST['ids']; 

      // Prepare and bind the udpate statement 
      $sql2 = "UPDATE Wishlists SET Picture = ?, Stage = ?, Gender = ?, Frozen = ?, Notes= ? WHERE ID = ?"; 
      $stmt2 = $conn->prepare($sql2); 
      $stmt2->bind_param('sssssi', $picture, $stage, $gender, $frozen, $notes, $id); 

      foreach($ids as $id){ 
       $stagecode = $id . "stage"; 
       $gendercode = $id . "gender"; 
       $frozencode = $id . "frozen"; 
       $notescode = $id . "notes"; 
       $namecode = $id . "creature"; 
       $stage = $_POST[$stagecode]; 
       $Stage = $stage; 
       $gender = $_POST[$gendercode]; 
       $frozen = $_POST[$frozencode]; 
       $notes = $_POST[$notescode]; 
       $name = $_POST[$namecode]; 

       $sql1 = 'SELECT * FROM Creatures WHERE Name = "' . $name . '"'; 
       $result = mysqli_query($conn, $sql1); 
       $row = $result->fetch_assoc(); 
       $picture = $row["$stage"]; 

       $stmt2->execute(); 
      } 

      $theCount = 0; 
      foreach($_POST['delete'] as $selected){ 
       $sql = "DELETE FROM Wishlists WHERE ID = ?"; 
       $stmt = $conn->prepare($sql); 
       $stmt->bind_param('i', $selected); 
       $stmt->execute(); 
       $theCount++; 
      } 
      echo "Your wishlist has been updated, and" .$theCount. " creature(s) has/have been removed from your wishlist.<br>Please click <a href='http://eggcavity.com/edit-wishlist'>here</a> to return to the edit page."; 
     } else { 
      // Get current user's username 
      $current_user = wp_get_current_user(); 
      $username = $current_user->user_login; 
      $theDeleteCount = 0; 
      // Just display the form 
      $sql = 'SELECT Creature, Picture, Stage, Gender, Frozen, ID FROM Wishlists WHERE Username = "' . $username . '"'; 
      $result = mysqli_query($conn, $sql); 
      if (mysqli_num_rows($result) > 0) { 
       echo '<form method="POST"><table><strong>' . 
        '<tr>' . 
         '<td></td>' . 
         '<td>Creature</td>' . 
         '<td>Stage</td>' . 
         '<td>Gender</td>' . 
         '<td>Frozen</td>' . 
        '</tr></strong>'; 
       while($row = $result->fetch_assoc()) { 
        $creature = $row["Creature"]; 
        $id = $row["ID"]; 
        $picture = $row["Picture"]; 
        $stage = $row["Stage"]; 
        echo '<input name="ids[]" type="hidden" value="' . $id . '">' . 
         '<input name="' . $id . 'creature" type="hidden" value="' . $creature . '">' . 
         '<tr>' . 
          '<td rowspan="2"><img src="' . $picture . '"></td>' . 
          '<td>' . $creature . '</td>' . 
          '<td><select name="' . $id . 'stage">' . 
           '<option value ="' . $stage . '" selected>' . $stage . '</option>' . 
           '<option value = "Stage1">Stage1(Egg)</option>' . 
           '<option value = "Stage2">Stage2</option>' . 
           '<option value = "Stage3">Stage3</option>' . 
           '<option value = "Stage4">Stage4</option>' . 
          '</select></td>' . 
          '<td><select name="' . $id . 'gender">' . 
           '<option value ="' . $row["Gender"] . '" selected>' . $row["Gender"] . '</option>' . 
           '<option value = "Unspecified">Unspecified</option>' . 
           '<option value = "Female">Female</option>' . 
           '<option value = "Male">Male</option>' . 
          '</select></td>' . 
          '<td><select name="' . $id . 'frozen">' . 
           '<option value ="' . $row["Frozen"] . '" selected>' . $row["Frozen"] . '</option>' . 
           '<option value="Unspecified">Unspecified</option>' . 
           '<option value="Yes">Yes</option>' . 
           '<option value="No">No</option>' . 
          '</select></td>' . 
         '</tr>' . 
         '<tr>' . 
          '<td colspan="3">Notes: <input type="text" name="' . $id . 'notes" value="' . $row["Notes"] .'"></td>' . 
          '<td>' . 'Delete<br>' . '<input type="checkbox" name="creatures[]" value="' . $id . '"></td>' . 
         '</tr>'; 
       } 
       echo '</table><input name="submit" type="submit" id="submit" value="Update"></form>'; 
      } else { 
       echo "<br>You have no creatures in your wishlist."; 
      } 
     } 
    } catch (mysqli_sql_exception $e) { 
     throw $e; 
    } 

    // Close the connection to the database 
    $conn->close(); 

如果你能幫我找到什麼是錯的,我傳遞給在foreach()語句的信息:

foreach($_POST['delete'] as $selected){ 

我將永遠感激。任何想法都有幫助

我已經嘗試了很多東西,其中很多都是在stackoverflow上找到的。我想我可能錯過了一些小小的和/或愚蠢的東西。我有另一個頁面運行的複選框表單,工作得很好。

謝謝你,祝你有美好的一天!

+0

很可能會出現POST陣列的所有表單提交中沒有'delete'所以你應該大概在循環之前測試一下 – RamRaider

回答

1

包含的ID的表單元素要刪除的文件被稱爲creatures[],因此您需要處理該POST變量的內容而不是delete - 即使delete是您想要執行的操作。這樣,也許是這樣的: -

更換

$theCount = 0; 
foreach($_POST['delete'] as $selected){ 
    $sql = "DELETE FROM Wishlists WHERE ID = ?"; 
    $stmt = $conn->prepare($sql); 
    $stmt->bind_param('i', $selected); 
    $stmt->execute(); 
    $theCount++; 
} 

$theCount = 0; 
$creatures=!empty($_POST['creatures']) ? $_POST['creatures'] : false; 
if($creatures) { 
    if(!is_array($creatures)) $creatures=explode(',',$creatures); 
    foreach($creatures as $id){ 
     $sql = "DELETE FROM Wishlists WHERE ID = ?"; 
     $stmt = $conn->prepare($sql); 
     $stmt->bind_param('i', $id); 
     $stmt->execute(); 
     $theCount++; 
    } 
} 
+0

使用你的代碼,如果選中它將刪除生物,但是如果沒有選擇它,它會顯示相同的錯誤。當我用上面的用戶提供的代碼嘗試您的答案時,它的工作原理! – TurtleBo

+0

哦是啊..忘了一件事...把整個地段的大括號...好景點@TurtleBo – RamRaider

+0

謝謝!非常! – TurtleBo

1

如果刪除是可選的每一次則只是把一個變量檢查​​像

if(isset($_POST['creatures'])) 
{ 
foreach($_POST['creatures'] as $selected){ 
       $sql = "DELETE FROM Wishlists WHERE ID = ?"; 
       $stmt = $conn->prepare($sql); 
       $stmt->bind_param('i', $selected); 
       $stmt->execute(); 
       $theCount++; 
      } 
} 

當它發現$_POST['creatures']這個代碼將只運行意味着UR複選框被選中

+0

謝謝!正是我所需要的 – TurtleBo

+0

除了現在不會刪除 – TurtleBo

+0

更改您的複選框輸入名稱以刪除 –