I'm working on a simple messaging system, and it's up until it works, however whenever a refresh is given on the page the information from the last insert is forwarded to the database. And if nothing has been filled, and given refresh it simply sends an empty value to the database, just as if you leave the page and go back to it the empty values are sent in the same way. I searched a lot in google and tested a lot of things, but nothing worked so far, I found some that tried to undo replicated data, however in that case I would like it to be sent duplicate anyway if it is done by submit by the user. Anyone know if you can differentiate submit from refresh?
I put an image showing the page and the error message that appears when given F5, this is not unique to firefox. Blank messages with a blank name are what I said earlier.
Follow the code:
Danilo Macedo Bakun
<div id="tudo">
<form id="formulario" action="comentarios.php" method="POST">
<h1>Nome:</h1>
<input type="text" id="nome" name="Nome" required><br>
<h1>Mensagem: </h1><br>
<textarea id="mensagem" name="Mensagem" required></textarea><br>
<input type="submit" name="enviar" value="Enviar">
</form>
<?php
$strcon = mysqli_connect('localhost','root','','teste') or die('Erro ao conectar ao banco de dados');
if (isset($_POST['Nome']) === true)
{
$nome = $_POST['Nome'];
} else {
$nome = false;
}
if (isset($_POST['Mensagem']) === true)
{
$mensagem = $_POST['Mensagem'];
} else {
$mensagem = false;
}
$sql = "INSERT INTO mensagem VALUES ('$nome', '$mensagem')";
mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
$consulta = "SELECT * FROM mensagem";
$sqlc = mysqli_query($strcon,$consulta) or die("Erro ao tentar consultar registro");
while($aux = mysqli_fetch_assoc($sqlc))
{
echo "<h1>-----------------------------------------</h1><br>";
echo "<h1>Nome: ".$aux["nome"]."</h1><br>";
echo "<h1>Mensagem:".$aux["mensagem"]."</h1><br>";
}
mysqli_close($strcon);
?>
</div>
</body>