如何确保在检查时仅将值保存到表中?

如何确保在检查时仅将值保存到表中?

问题描述:

用我的代码在这里,如果用户选中名称 其中一个名称旁边的复选框,则当用户单击“保存”时,名称将保存到我的review_shared表中。如何确保在检查时仅将值保存到表中?

但我想要一个选中的值只保存 一次到表中,现在它被保存多次 - 我的意思是,如果用户回到这个页面,名称被选中,用户点击再次保存。

enter image description here

我可以添加到代码中要做到这一点什么,所以它只会在我review_shared表保存一次?目前,它看起来是这样的,它多次将其保存:

if(!empty($_POST['check_contacts'])) { 
    foreach($_POST['check_contacts'] as $check) { 

     //$_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); 

    } 

     //go to the main page when changes have been saved 
    header('Location:volleyLogin.php'); 
} 

    $con->close(); 

我想加入这样的代码到上面会有所帮助,但事实并非如此,真正做到:

 if(!empty($_POST['check_contacts'])) { 

    //************************* added this in ************** 
       $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"; 
      break; 
      } 

//******************************************************* 

     foreach($_POST['check_contacts'] as $check) { 

      //$_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); 

     } 

      //go to the main page when changes have been saved 
     header('Location:volleyLogin.php'); 
    } 

     $con->close(); 
+0

这是更好地使字段名称独特的字段,这样就可以让数据库面对这一切。使用'insert ... on duplicate key update'。不需要这样的初始查询。 – apokryfos

break;使用来自循环(for,foreach,whiledo-while)和switch结构,而不是从if块。你必须创建一个对应于else块,即if块,并将整个foreach循环放在那里。

// your code 
if($num_rows) { 
    echo "This is already a contact"; 
}else{ 
    foreach($_POST['check_contacts'] as $check) { 

     //$_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); 

    } 
} 
// your code 

以下是相关的参考:

+0

好的,有用的代码。有了它,我解决了我的问题,将很快发布。它实际上是一个IF和ELSE IF块,我使用。 – CHarris

+1

@ChristopheHarris很高兴的答案帮助你解决了这个问题。 :-) –