Php 如何取消选中数据库中的值?

Php 如何取消选中数据库中的值?,php,checkbox,mysqli,Php,Checkbox,Mysqli,对于我的代码,如果用户选中其中一个名称旁边的复选框 该名称将保存到我的review\u shared表中 我正在尝试使用我的代码,以便在未选中某个框时,该名称将从review_共享表中删除。 你知道我该怎么做,或者从哪里开始?因此,如果未选中Paul Thompson和/或Dan Frain,它们将从我的review\u shared表中删除 <?php //here we want to save the checked contacts to the review_s

对于我的代码,如果用户选中其中一个名称旁边的复选框 该名称将保存到我的
review\u shared
表中

我正在尝试使用我的代码,以便在未选中某个框时,该名称将从review_共享表中删除。 你知道我该怎么做,或者从哪里开始?因此,如果未选中
Paul Thompson
和/或
Dan Frain
,它们将从我的
review\u shared
表中删除

    <?php
    //here we want to save the checked contacts to the review_shared table ;  that is,
    //who the user wants to share reviews with
    if(!empty($_POST['check_contacts'])) {
        foreach($_POST['check_contacts'] as $check) {

                    //$check="";
        //if the contact in review_shared is already checked, we don't want to save it multiple times
            $already_checked = "SELECT * from review_shared WHERE user_id = '$user_id' AND contact_id = '$check' AND review_id = " .$_GET['id'];

            $already_checked_result=mysqli_query($con,$already_checked);
            $num_rows = mysqli_num_rows($already_checked_result);

            if($num_rows >= 1) {
            echo "This is already a contact";
            }

        else if ($num_rows < 1) {

            //$_GET['id'] is the current review for which contacts are being edited, we are checking a contact to share that review with
                $insert_review_shared_command = "INSERT INTO review_shared VALUES(NULL," .$_GET['id']. ", '$user_id','$check')";

            //we want to save the checked contacts into the review_shared table
            $insert_into_review_shared_table = mysqli_query($con,$insert_review_shared_command);

        }

      }
   }

        $con->close();


        ?> 

实现所需的唯一方法是

  • 删除所有条目
  • 插入选中的选项
保存表单数据时

要删除以前的所有条目,请执行以下操作

$sql = "DELETE FROM review_shared WHERE your-conditions";

这是因为当您发布复选框时,您将只在服务器端收到选中的复选框。这些都是您需要存储的数据。

多亏了上面的Simone Cabrino,我只是在代码中添加了以下内容:

        $already_checked = "DELETE from review_shared WHERE user_id = '$user_id' AND review_id = " .$_GET['id'];
        mysqli_query($con,$already_checked);

啊,很有趣。这么简单!不要添加答案,只需添加一条注释并接受正确的答案。如果StackOverFlow可以让在注释中放置代码变得更容易,那就太好了。