I am working with a simple user registration form, they are already inserted, the code
HTML
<?php include '../includes/insert/insert_usuario.php'; ?>
<form class="form-horizontal style-form" method="post">
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label"><b>Nombre</b></label>
<div class="col-sm-3">
<input type="text" class="form-control" minlength="3" name="name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label"><b>Correo</b></label>
<div class="col-sm-3">
<input type="email" class="form-control grey" name="email">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label"><b>Contraseña</b></label>
<div class="col-sm-3">
<input type="password" class="form-control grey" minlength="5" name="password">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label"><b>Confirmar contraseña</b></label>
<div class="col-sm-3">
<input type="password" class="form-control grey" minlength="5" name="cPassword">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label"></label>
<div class="col-sm-9">
<button class="btn btn-primary" name="submit" type="submit"><i class="fa fa-floppy-o"></i> GUARDAR</button>
</div>
</div>
</form>
PHP (insert_user.php)
<?php
$msg = "";
if (isset($_POST['submit'])) {
$con = new mysqli('localhost', 'root', '', 'carreteras');
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$cPassword = $_POST['cPassword'];
if ($password != $cPassword)
$msg = "¡Las contraseñas no coinciden!";
else {
$hash = password_hash($password, PASSWORD_BCRYPT);
$con->query("INSERT INTO users (name,email,password) VALUES ('$name', '$email', '$hash')");
$msg = "¡Usuario registrado con éxito!";
}
}
?>
I have two drawbacks, the first one is that when I press submit and the passwords do not match, the fields in my form are cleaned, how can I do so that this does not happen and allow the user to edit their information. The second is that if the INSERT proceeds, the following happens: the fields are "apparently" cleaned, the message of "Successfully registered user" is displayed! but the message stays there and if I refresh the page with f5 the same record is reinserted into my database, even though it apparently has been cleaned.
I hope you can help me, I thank you from now on.