When submitting the data, it always shows me the following custom error message:
An error has occurred internally, please try to submit your request again later
I have no more details to attach, it does not show me any other error.
<?php
//session_start();
include "app/php/require.ini.php";
if (isset($_POST['formsubmitted'])) {
$msg = array();
if (empty($_POST['username'])) {
$msg[] = 'Por favor, ingrese un nombre de usuario';
} else {
$username = $_POST['username'];
}
if (empty($_POST['email'])) {
$msg[] = 'Por favor, ingrese su correo electrónico';
} else {
if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['email'])) {
$email = $_POST['email'];
} else {
$msg[] = 'Tu dirección de correo electrónico no es válida';
}
}
if (strlen($_POST['password']) <6){
$msg[] = 'Su contraseña debe tener al menos 6 caracteres';
}
if ($_POST['password'] !== $_POST['password_again']){
$msg[] = 'Su contraseña no coincide';
} else {
$password = $_POST['password'];
}
if (empty($_POST['firstname'])) {
$msg[] = 'Por favor, ingrese su nombre';
} else {
$first_name = $_POST['firstname'];
}
if (empty($msg)) {
$stmt = $con->prepare("SELECT * FROM users WHERE email=? OR username=?");
$stmt->bind_param("ss",$email,$username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows>0) {
echo "¡El usuario con este correo electrónico ya existe!";
} else {
$hash_password = password_hash($password, CRYPT_BLOWFISH);
$key = bin2hex(openssl_random_pseudo_bytes(32));
//$key_two = bin2hex(random_bytes(32)); // Disponible apartir de PHP V.7
//$active_default = 0;
$stmtA = $con->prepare("INSERT INTO users (username, email, password, first_name, email_code) VALUES (?, ?, ?, ?, ?)");
$stmtA->bind_param("sssss", $username,$email,$hash_password,$first_name,$key);
if($stmtA->execute()){
echo 'El enlace de confirmación ha sido enviado por correo electrónico. ¡Por favor, haga clic en el enlace del mensaje para activar su cuenta!';
$to = $email;
$subject = "Por favor, verifique su cuenta.";
$message_body = 'Hola '.$first_name.',
¡Gracias por registrarte!
Estas aún solo paso de ser parte de nuestra comunidad.
Por favor, haga clic en este enlace para activar su cuenta:
http://localhost/login-system/verify.php?email='.urlencode($email).'&key='.$key.'';
mail($to, $subject, $message_body, 'From: [email protected]');
header("location: index.php");
exit;
} else {
echo "Ha ocurrido un error internamente, por favor, vuelva intertar enviar su solicitud más tarde";
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<form action="register.php" method="post">
<input type="text" id="username" name="username" placeholder="Usuario" value="<?php echo isset($username) ? $username: ""; ?>" >
<input type="text" id="email" name="email" placeholder="Email" value="<?php echo isset($email) ? $email: ""; ?>" >
<input type="password" id="password" name="password" placeholder="Password">
<input type="password" id="password_again" name="password_again" placeholder="Password again">
<input type="text" id="firstname" name="firstname" placeholder="Nombre" value="<?php echo isset($first_name) ? $first_name: ""; ?>" >
<input type="submit" name="formsubmitted" value="Register" />
</form>
</body>
</html>
Update
The custom error message is no longer displayed by making the following changes, replacing: if (empty($error)) {}
byif (empty($msg)) {}
But now, when submitting the blank form data, that is, without filling in the fields input
, it does not show me any error message.
In order for the error messages that have occurred during submission to be displayed after the form is submitted, you should add the following somewhere in your HTML:
Another alternative way to implement it is: