When validating the creation of a new user, before performing the INSERT in the users table, I perform 2 queries to check if the name and email exist.
If there are matches in the name, it works OK. If there are matches in the email, it doesn't work. If there are matches in both fields, it finds a match in the email.
Thanks.
I attach the code:
/* Creamos la conexión BD */
include("../php/conectionBD.php");
/* Validaciones... */
/* Eliminamos la variable en sesión para el control de errores */
unset($SESSION["errorCreateAccount"]);
/* Comprobamos si el nombre de usuario existe */
$queryUsername = $db->prepare("SELECT * FROM `users` WHERE `username` = ?;");
$queryUsername->bindParam(1, $username);
$queryUsername->execute();
if ($queryUsername->rowCount() > 0){
/* Crear variable de Sesión para el error */
$SESSION["errorCreateAccount"] = 'ErrorUsername';
/* Redireccionamos a la página de registro */
header('location:../pages/createAccount1.php');
}
/* Comprobamos si el email existe */
$queryEmail = $db->prepare("SELECT * FROM `users` WHERE `email` = ?;");
$queryEmail->bindParam(1, $email);
$queryEmail->execute();
if ($queryEmail->rowCount() > 0){
/* Crear variable de Sesión para el error */
$SESSION["errorCreateAccount"] = 'ErrorEmail';
/* Redireccionamos a la página de registro */
header('location:../pages/createAccount2.php');
}
/* Fin Validaciones... */
I have modified the code, you must define the types of parameters that you are going to bind together with your sentence, in this case
string
.You also have an error in the two prepare statements at the end as you added a ; in
= ?;"
you must also remove the;
To check if there are any e-records used
store_result()
, pass a result set from a prepared statement.To check if any results exist, I use
num_rows
Finally, it is also important as soon as we finish with our prepared sentence, to close said sentence, simply with
close()
Looking at the documentation I see that one thing that may be wrong is
1
what you put in thebindParam
. Try replacing it with"s"
--> Important to be in""
Another possible problem is that the
close()
statement is not done:Likewise, why not make a single query to the DB, you save queries and time: