如何在每次刷新时停止插入MySQL

时间:2016-10-26 00:14:10

标签: php mysql

每次刷新它都会发布一条新消息,我知道这是因为我正在使用“$ _SERVER [”PHP_SELF“]”,我如何在每次访问页面时都不使用它而不发布新的空消息?     

$a = "localhost";
$b = "root";
$c = "";
$d = "database";

$connect = new mysqli($a, $b, $c, $d);

if ($connect->connect_error) {
    die("Connection failed: " . $connect->connect_error);
}

echo "Connected successfully.<br/>";

$sql = "INSERT INTO messages (message)
VALUES ('$_POST[message]')";

if ($connect->query($sql) === TRUE) {
    echo "New record created successfully.<br/>";
} else {
    echo "Error: " . $sql . "<br/>" . $connect->error;
}

$newsql = "SELECT id, message, message_date FROM messages";
$result = $connect->query($newsql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . ", Message: " . $row["message"] . ", Posted: " . $row["message_date"] . ", IP: " . $_SERVER["REMOTE_ADDR"] . "<br/>";
    }
} else {
    echo "No messages to display.<br/>";
}

?>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
    <input type="text" name="message">
    <input type="submit">
</form>

0 个答案:

没有答案