我正在制作一个注册页面,我在检查用户名是否已经在mysql表中时已经出现问题了。这是我使用的代码:
$check = mysqli_query($con,"SELECT * FROM users
WHERE username='$username'");
if(!empty($check)){
$_SESSION['error'] = "Username allready taken!";
header('Location: ./?page=signup') ;
}
任何人都可以帮我吗?
答案 0 :(得分:2)
$userExists = (mysqli_num_rows($check) > 0);
答案 1 :(得分:2)
您应该使用预准备语句来编写查询。但是由于原始mysqli无法使用它们,因此最好使用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') ;
}
答案 2 :(得分:-1)
好的人你成功地没有回答我的问题,所以我想出了自己,这是最后的答案:
$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') ;
}