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>
Das war der Grund. Problem gelöst, danke! – Mateusz