php 基于多选列表向数据库中删除和添加项目

q9yhzks0  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(146)

我目前正在尝试创建一个多选列表,其中可以基于两列添加和删除项目。左列是完整列表,右列显示所有选定项目。提交时,右列将所有新选定项目添加到USERVIDEOS数据库中。
但是,如果从右列中删除项目,则会从USERVIDEOS数据库中删除项目。按“提交”时,USERVIDEOS数据库应仅包含当前位于右列中的项目的行,并删除不再位于右列中的项目。

示例图像

我有几个项目,但我无法解决这个问题。我已经尝试了以下代码:

if($_SERVER["REQUEST_METHOD"] == "POST") {
        if (isset($_POST['user-videos'])) {
  
          try {
              $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
              $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $stmt = $conn->prepare("DELETE FROM USERVIDEOS WHERE userID = :userID AND videoID NOT IN (:videos)");
              $stmt->bindParam(':userID', $userID);
              $stmt->bindValue(':videos', implode(",", $_POST['user-videos']));
              $stmt->execute();
              // Add the new Videos
              $stmt = $conn->prepare("INSERT INTO USERVIDEOS (userID, videoID) VALUES (:userID, :videoID)");
              $stmt->bindParam(':userID', $userID);
              $selected = $_POST['user-videos'];
              foreach ($selected as $videoID) {
                  $stmt->bindParam(':videoID', $videoID);
                  $stmt->execute();
              }
          } catch (PDOException $e) {
              echo 'Error: ' . $e->getMessage();
          }

然而,这会导致删除除了新选择的项目之外的所有项目。2目标是只删除那些从右列中删除的项目,并添加那些添加到右列中的项目。

超文本标记语言

<div class="form_container">
        <form name="videos" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <div class="lvids">
          <label for="video-list">Database Videos:</label>
          <select class="videolist" id="video-list" size="10" name="video-list[]" multiple>
            <?php
              try {
                $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                // Retrieve all video IDs from the USERVIDEOS table
                $stmt1 = $conn->prepare("SELECT videoID FROM USERVIDEOS WHERE userID = :userID");
                $stmt1->bindParam(':userID', $userID);
                $stmt1->execute();
                $filterVideos = $stmt1->fetchAll(PDO::FETCH_COLUMN, 0);

                if(sizeof($filterVideos) != 0) {
                  $stmt2 = $conn->prepare("SELECT ID, videotitle FROM VIDEOS WHERE ID NOT IN (".implode(',', $filterVideos).") ORDER BY categorie ASC");
                  $stmt2->execute();
                  while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
                      echo "<option value='" . $row['ID'] . "'>" . $row['videotitle'] . "</option>";
                }
                } else {
                  $stmt = $conn->prepare("SELECT ID, videotitle FROM VIDEOS ORDER BY categorie ASC");
                  $stmt->execute();
                  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                      echo "<option value='" . $row['ID'] . "'>" . $row['videotitle'] . "</option>";
                  }
                }         
              } catch (PDOException $e) {
                echo 'Error: ' . $e->getMessage();
              }

              $conn = null;
              $stmt = null;
            ?>

          </select>
        </div>

        <div class="button_container">
          <input class="btn" type="button" value=">>" id="add-btn">
          <input class="btn" type="button" value="<<" id="remove-btn">
        </div>

          <div class="uvids">
            <label for="user-videos">Selected Videos:</label>
            <select class="uservideos" id="user-videos" size="10" name="user-videos[]" multiple>
              <?php 
              
              try {
                $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $stmt = $conn->prepare("SELECT VIDEOS.ID, VIDEOS.videotitle FROM VIDEOS 
                                        JOIN USERVIDEOS ON USERVIDEOS.videoID = VIDEOS.ID 
                                        WHERE USERVIDEOS.userID = :userID");
                $stmt->bindParam(':userID', $userID);
                $stmt->execute();
                $result = $stmt->fetchAll();
                foreach($result as $row) {
                    echo "<option value='" . $row['ID'] . "'>" . $row['videotitle'] . "</option>";
                }
            } catch (PDOException $e) {
                echo 'Error: ' . $e->getMessage();
            }
              
              ?>
            </select>
          </div>

          <br><br>
          <input type="submit" value="Upload Videos">
        </form>
      </div>

如果需要更多信息,请直接询问。

xxhby3vn

xxhby3vn1#

我找到了解决这个问题的(非理想的)办法。
我使用javascript在提交按钮的右列中选择所有选项,然后PHP删除数据库中userID = $userID的所有条目,然后将所有选择的选项添加回去。
这个问题已经通过一个变通方法得到了解决。但是,理想情况下,数据库应该删除所有内容,然后重新添加以前存在的项。

使用Javascript选择select中的所有选项:

$('#submit-button').click(function(event) {
     event.preventDefault(); // prevent the form from being submitted

     $('#user-videos option').prop('selected', true);
     $('#videos').submit(); 
});

相关问题