2017-01-06 4 views
0

Ich brauche eine Hilfe mit meinem Code; Irgendwie schafft mein Code zwei Räume (er fügt zwei Reihen gleichzeitig in eine Tabelle ein), ich weiß nicht warum.PHP/Mysqli: Warum verdoppelt dieser Code Zeilen Zeilen einfügen?

(Ich brauche eine ID für jeden Einsatz zu wissen, in welchem ​​Haus wir einen neuen Raum erstellen. Meine Datenbank enthält Tabelle 'Haus' und Tabelle 'Raum'. Tabelle 'Raum' hat ein Feld 'Haus_ID' das ist ein Fremdschlüssel mit einem Feld 'id' in der Tabelle 'Haus')

das ist meine pHP-Seite ist.

<?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); 

?> 

und das ist meine hTML-Vorlage mit Form:

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

Antwort

-1

Nicht 100% sicher, aber es sieht wie y aus Sie führen die INSERT-Abfrage zweimal aus. Einmal hier:

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

und einmal im nächsten Moment, wenn Sie versuchen, nach etwas zu suchen. Versehen Sie die OOP-Stil verwenden, wenn Sie offenbar für etwas

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

Es fügt zwei Zeilen zu überprüfen versuchen, wegen Ihrer zweimal die Abfrage-Funktion:

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

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

So müssen Sie das ändern bedingte Anweisung zu:

if ($result) { 

By the way, verwenden sie eine vorbereitete Erklärung, es ist sicherer als real_escape_string():

0

Du Einsetzen zweimal

hier zuerst:

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

dann hier:

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

die erste entfernen und Sie sollten in Ordnung sein oder überprüfen Sie die Ergebnis der ersten und entfernen Sie die zweite.

+0

Das war der Grund. Problem gelöst, danke! – Mateusz