我有以下代码来检查username
是否已被使用,并且我需要再使用4个字段来完成此操作。
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql2 = "SELECT * FROM users WHERE username = ?";
$q2 = $pdo->prepare($sql2);
$q2->execute(array($username));
$result = $q2->fetchAll();
if (count($result) > 0) {
$usernameError = 'O utilizador já existe!';
}
else{
$sql = "INSERT INTO users (username,password,role) values(?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($username,$password,$role));
header("Location: index.php");
}
Database::disconnect();
}
答案 0 :(得分:0)
如果您需要知道哪些字段已经重复,请使用以下命令:
SELECT MAX(username = :username) AS usernameError, MAX(email = :email) AS emailError, MAX(phone = :phone) AS phoneError
FROM users
WHERE username = :username OR email = :email or phone = :phone
然后,您可以为结果中等于1
的每一列生成适当的错误消息。