2016-12-09 11 views
0

私のコードは、ユーザーからの簡単なテキストとタイトルを取り込むためのもので、データベースに2回追加している理由を理解できません。PHPが1回実行され、sqlが2回挿入される

<?php ("session.php"); ?> 
    <?php require_once("connect.php"); ?> 
    <?php include("header.php"); ?>    

    <?php 


     //if the submit button is pressed then the INSERT SQL statement is submitted to database 
     //if (isset($_POST['testimonial_title']) && isset($_SESSION['testimonial_text'])) { 

     if(isset($_POST["submit_button"])){ 



      $title=$_POST["testimonial_title"]; 
      $text=$_POST["testimonial_text"]; 



      //create the sql statement 
      $sql= 
       "INSERT INTO testimonials 
       (testimonial_title, testimonial_text, student_ID) 
       VALUES(
       '$title', 
       '$text', 
       1);"; //for simplicity the student_ID is kept the same 


      $result = mysqli_query($con,$sql); 
      if(!mysqli_query($con, $sql)) 
      { 
       echo "Journal entry was not entered successfully"; 
      } 
      else{ 
       echo "Journal entry was entered successfully"; 
      } 

      mysqli_close($con); 
     } 

    ?> 


    <html> 
     <body> 

     <!-- Page Content --> 
        <h1 class="page-header">Learning Journals 
         <small>- Admin</small> 
        </h1> 

        <h3>Update Learning Journals Plans</h3> 

        <form name="membership_form" action= "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="contactForm" method="post"> 

         <label>Learning Journals Title:</label> 
         <br> 
          <input type="text" name="testimonial_title"> 
         <br> 

          <label>Learning Journal:</label> 
          <br> 
          <textarea rows="10" cols="100" name="testimonial_text" maxlength="999" style="resize:none"></textarea> 
          <br> 
         <button type="submit" name ="submit_button" class="btn btn-primary">Update</button> 
        </form> 
     </body> 
    </html> 

何起こるべきことは次のとおりです。

ID | title | text | student_ID| 

1 | xyz | xyz | 1   | 

しかし、その代わりに、それ:

ユーザーは

ユーザーがテキスト "xyzは"

データベースを取り込んで入力するタイトル "XYZ" を入力データベースは次のようになります。

ID | title | text | student_ID| 

1 | xyz | xyz | 1   | 

2 | xyz | xyz | 1   | 
+0

これはあなたの答えではありませんが、 'ALTER TABLEの声は制約がユニーク(タイトルをuc_testimonials追加のように私はあなたのテーブルに制約を追加することをお勧めします、テキスト、studentid) 'または'(タイトル、studentid) ' – SqlZim

答えて

5

変更:

 $result = mysqli_query($con,$sql); 
     if(!mysqli_query($con, $sql)) 

へ:

 $result = mysqli_query($con,$sql); 
     if(!$result) 
関連する問題