I'm doing a class work in which they ask me to make a record system, to insert the user's data in a database, I have the following function:
function comprovaUsuari($user, $passwd){
$servername = "localhost";
$username = "root";
$dbpasswd = "";
$conn = new PDO("mysql:host=$servername;dbname=php",$username, $dbpasswd);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM users WHERE username='$user'";
$rows = $conn->query($sql);
$rows = $rows -> fetch();
return $CORRECTE = ($rows==0);
}
This was the last thing I tried, then I have a conditional, if the variable CORRECT is true, it will tell me an error, but otherwise, it will insert the records in the table:
$CORRECTE = comprovaUsuari($user, $pass);
if ($CORRECTE){
setcookie("error", "Nom d'usuari ja ocupat, torna a intentar", time() + 2, "/");
header("Location: register.php");
}else{
afegirUsuari($user, $name, $pass);
}
The main problem I have is that I have been testing for a couple of days, but changing all kinds of conditions, I simply have two options, or let me introduce the same user infinitely, or do not let me enter any, when the idea is that I can only enter once "x" username, to see if someone can light me up a bit.
By the way, the fields in the users table are name, username, password and ID