带复选框的HTML表单元素

问题描述:

我有一些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){ 

我将永远感激。任何想法都有帮助

我已经尝试了很多东西,其中很多都是在*上找到的。我想我可能错过了一些小小的和/或愚蠢的东西。我有另一个页面运行的复选框表单,工作得很好。

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

+0

很可能会出现POST阵列的所有表单提交中没有'delete'所以你应该大概在循环之前测试一下 – RamRaider

包含的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

如果删除是可选的每一次则只是把一个变量检查​​像

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

更改您的复选框输入名称以删除 –