문제

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?

도움이 되었습니까?

해결책 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') ;
}

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top