2017-03-04 4 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

それはあなたがそれを使用してデータベースの契約をさせることができますので、フィールド名に固有のフィールドを作成すると良いでしょう:

// 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 

ここでは、関連する参照です。 '重複するキーの更新時に' insert ...を使用してください。このように初期クエリは必要ありません。 – apokryfos

答えて

1

break;が使用されています(for,foreach,whileおよびdo-while)およびswitch構造から出てくるものであり、ifブロックではありません。 ブロックに対応するelseブロックを作成して、ifブロックを作成し、そこにforeachループ全体を配置する必要があります。

+0

すばらしく役立つコードです。私は問題を解決し、すぐに投稿します。実際にはIFとELSE IFブロックです。 – CHarris

+1

@ChristopheHarris答えがあなたの問題を解決するのに役立ちました。 :-) –

関連する問題