2017-03-04 2 views
0

내 코드 여기에 사용자가 중 하나의 이름 옆에있는 확인란을 선택하면 사용자가 '저장'을 클릭하면 이름이 내 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

그것은 필드 이름 그래서 당신은 그와 데이터베이스 거래를 할 수있는 고유 필드 확인하는 것이 좋습니다. '중복 키 갱신시 삽입 ...을 사용하십시오. 이런 식으로 초기 쿼리가 필요 없습니다. – apokryfos

답변

1

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 질문에 답변 해 주셔서 감사합니다. :-) –

관련 문제