2017-01-06 6 views
0

私のコードで助けが必要です。どうにかして私のコードは2つの部屋を作成します(それは同時に2つの行をテーブルに挿入します)。なぜか分かりません。PHP/Mysqli:なぜこのコードは行の挿入を2倍にしますか?

(私は新しい部屋を作成する家を知るためにすべてのインサートにIDを要求する必要があります)データベースにはテーブル 'house'とテーブル 'room'が含まれています。 'room'テーブルは 'house_id'テーブルの家 "でフィールド 'ID' を持つ外部キー)

私のPHPページです。

<?php 
      // turn autocommit off 
      mysqli_autocommit($con, FALSE); 

      // fetch the houses so that we have access to their names and id 
      $query = "SELECT name, id 
        FROM house"; 
      $result = mysqli_query($con, $query); 
      // check query returned a result 
      if ($result === false) { 
       echo mysqli_error($con); 
      } else { 
       $options = ""; 
       // create an option 
       while ($row = mysqli_fetch_assoc($result)) { 
        // $options .= "".$row['name'].""; 
        $options .= "<option value='".$row['id']."'>"; 
        $options .= $row['name']; 
        $options .= "</option>"; 
       } 
      } 

      include('templates/add_room.html'); 

      if ($_SERVER["REQUEST_METHOD"] == "POST") { 
       $price = mysqli_real_escape_string($con, $_POST["price"]); 
       $house = mysqli_real_escape_string($con, $_POST["house_id"]); 

       $query = "INSERT INTO room (price, house_id) 
       VALUES ('$price', '$house')"; 

       // run the query to insert the data 
       $result = mysqli_query($con, $query); 

       // check if the query went ok 
       if ($con->query($query)) { 
        echo "<script type= 'text/javascript'>alert('New room created successfully with the id of {$con->insert_id}');</script>"; 
        mysqli_commit($con); 

       } else { 
        echo "There was a problem:<br />$query<br />{$con->error}"; 
        mysqli_rollback($con); 
       } 
      } 

      //free result set 
       mysqli_free_result($result); 

?> 

、それが形と私のHTMLテンプレートです:

<h2>Add new room</h2> 
<form action='' method='POST'> 
      <fieldset> 
       <label for='price'>Price:</label> 
       <input type='number' name='price'> 
      </fieldset> 
      <fieldset> 
      <label for='house_id'>House:</label> 
       <select name='house_id' required> 
        <option value='' disabled selected>Select house</options> 
        <?php echo $options; ?> 
       </select> 
      </fieldset> 
      <button type='submit'>Add</button> 
</form> 

答えて

-1

ありません100%確実だが、yのように見えるINSERTクエリを2回実行します。かつてここに:

$result = mysqli_query($con, $query); 

、あなたが何かをチェックしようとすると、一瞬後に一度

$result = mysqli_query($con, $query); 

       // check if the query went ok 
       if ($con->query($query)) { 

だから、あなたはそれを変更する必要があります:あなたが誤って使用すると、明らかに何か

if ($con->query($query)) { 
2

をチェックしようとしているとき、それが原因であなたが二回のクエリ機能を使用しての2行を挿入OOPスタイルを使用します条件文:ところで

if ($result) { 

、プリペアドステートメントを使用し、それがreal_escape_string()よりも安全です。

ここ
0

あなたはそれを2回挿入されている

最初:その後、ここ

// run the query to insert the data 
$result = mysqli_query($con, $query); 

// check if the query went ok 
if ($con->query($query)) { 

が最初の1つを削除して、あなたは問題ないはずです、または最初の結果と2番目の結果を削除します。

+0

これが理由です。問題は解決しました、ありがとう! – Mateusz

関連する問題