Ich arbeite an einer Chat-Web-Anwendung. Das Problem ist, dass jedes Mal, wenn die Seite geladen wird, eine leere Nachricht in die Datenbank eingefügt wird.Ein zusätzlicher Sql-Datensatz wird jedes Mal hinzugefügt
Der Code ist wie unten
<!-- Insert MySQL datbase into HTML -->
<?php
$connection = mysqli_connect("localhost", "root", "", "tru");
$query = "SELECT * FROM shouts ORDER BY id Desc LIMIT 8";
$shouts = mysqli_query($connection, $query);
?>
<!-- Insert MySQL datbase into HTML -->
<?php while ($row = mysqli_fetch_assoc($shouts)) : ?>
<li> <?php echo $row['shout']; ?> <b> Sent at </b><?php echo $row['Time']; ?></li>
<?php endwhile; ?>
</ul>
</div>
<footer>
<form action="index.php" method="post">
<label>Shout Text: </label>
<input type="text" name="shout" placeholder="Enter your message here">
<input type="submit" id="submit" value="SHOUT!" >
</form>
<?php
<!-- Insert into MySQL datbase -->
$link = mysqli_connect("localhost", "root", "", "tru");
$sql = "INSERT INTO shouts (name,shout) VALUES ('$_POST[name]','$_POST[shout]')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
</footer>
</div>
</body>
</html>
Es macht nur Sinn, wenn Sie eine INSERT INTO ... Anweisung haben, wird eine neue Zeile in Ihre Datenbank eingefügt. Ihr Code macht genau das, was Sie geschrieben haben. Was ist das Problem? – Dekel
@Dekel hat Recht. Jedes Mal, wenn Sie Ihre Seite neu laden, wird Ihr PHP-Code ausgeführt. Vielleicht möchten Sie eine Art von Bedingung setzen, bevor Sie –
einfügen, was würde diese Bedingung sein @ FranckNgako –