Question

I am making a signup page and i'm having problems checking if the username is allready in the mysql table. Here's the code i used:

$check = mysqli_query($con,"SELECT * FROM users
WHERE username='$username'");

if(!empty($check)){
    $_SESSION['error'] = "Username allready taken!";
    header('Location: ./?page=signup') ;
}

Can anyone help me with this?

Was it helpful?

Solution 3

Okay people you have successfully failed to awnser my question, so i figured it out my self and here is the final awnser:

$sql = mysqli_query($con, "SELECT * FROM users  
        WHERE username='$usr'
        LIMIT 1"); 

if(mysqli_num_rows($sql) == 1){ 
    $_SESSION['error'] = "Username allready taken!";
    header('Location: ./?page=signup') ;
}

OTHER TIPS

You should be using prepared statements for composing a query. But as raw mysqli is unusable with them, you better use PDO

$stmt = $pdo->prepare("SELECT 1 FROM users WHERE username=?");
$stmt->execute([$username]);
$check = $stmt->fetchColumn();
if($check)){
    $_SESSION['error'] = "Username allready taken!";
    header('Location: ./?page=signup') ;
}
$userExists = (mysqli_num_rows($check) > 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top