[英]Query mySQL to check if user already exists in two tables
我正在關注如何為網站創建用戶注冊系統的教程。 我已經完成了這一步,但我需要檢查兩個表中的多個值以查看它們是否存在。 本教程不這樣做。
這是我的注冊表:
<form action="/members/register/" name="registerForm">
<div>
<h2>Register</h2>
<h6>welcome</h6>
<div class="register">
<input type="text" name="firstname" placeholder="First Name" required>
<input type="text" name="lastname" placeholder="Last Name" required>
<input type="text" name="email" placeholder="Email" required>
<label for="dob">Date of Birth:
<input type="date" name="dob" id="dob" placeholder="Date of Birth" required>
</label>
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<input type="password" name="passwordconfirm" placeholder="Confirm Password" required>
<p>By creating an account you agree to our <a href="/legal/terms-of-use/">Terms & Privacy</a>.</p>
<button type="submit" name="registerNow">Register</button>
</div>
</div>
</form>
我需要檢查的 mySQL 數據庫表是:
users
-id
-username
-password
-userID (foreign key)
registered
-id
-nameFirst
-nameLast
-email
-dob
我需要創建一個查詢來檢查以下是否已經存在:1)名字和姓氏在一起,2)用戶名,或 3)email。
此外,一旦我了解了如何執行這樣的查詢,我仍然對?
在查詢中。 此外,此代碼示例僅檢查用戶名是否存在並輸出“用戶名已存在”。 請選擇另一個。 我需要根據表中已經存在的字段來 output 不同的東西。 這是教程中的代碼:
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
// Store the result so we can check if the account exists in the database.
if ($stmt->num_rows > 0) {
// Username already exists
echo 'Username already exists. Please choose another.';
} else {
// Insert new account
}
$stmt->close();
} else {
// Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
echo 'Could not prepare statement!';
}
$con->close();
會是這樣嗎?
SELECT id, password
FROM users, registered
WHERE users.username = ? OR (registered.nameFirst = ? AND registered.nameLast = ?) OR registered.email = ?
再一次,我正在學習如何使用教程來做到這一點,所以我不想就代碼的操作方式對代碼進行任何更改。 我知道有更好的方法可以做到這一點。 我以此為起點來學習和進步。
由於您要做的就是檢查注冊值是否已經存在,因此使用EXISTS
子查詢而不是JOIN
將兩個表放在一起可能是最簡單的。 像這樣的東西:
SELECT
EXISTS (SELECT * FROM users WHERE username = ?) AS found_username,
EXISTS (SELECT * FROM registered WHERE nameFirst = ? AND nameLast = ?) AS found_name,
EXISTS (SELECT * FROM registered WHERE email = ?) AS found_email
在這個查詢中?
代表用戶名、名字、姓氏和 email 值的占位符。 使用帶有占位符的准備好的語句的目的是防止 SQL 注入,有關更多信息,請參閱此 Q&A 。 使用它們還有一個好處是無需轉義輸入中的特殊字符(例如,如果您想使用單引號括起來的值將O'Hara
插入到 nameLast 字段中)。
因此,對於您的代碼,您可以執行以下操作:
if ($stmt = $con->prepare('SELECT
EXISTS (SELECT * FROM users WHERE username = ?) AS found_username,
EXISTS (SELECT * FROM registered WHERE nameFirst = ? AND nameLast = ?) AS found_name,
EXISTS (SELECT * FROM registered WHERE email = ?) AS found_email')) {
// Bind parameters (s = string, i = int, b = blob, etc)
$stmt->bind_param('ssss', $_POST['username'], $_POST['firstname'], $_POST['lastname'], $_POST['email']);
$stmt->execute();
$stmt->bind_result($found_username, $found_name, $found_email);
$stmt->fetch();
// Store the result so we can check if the account exists in the database.
if ($found_username) {
// Username already exists
echo 'Username already exists. Please choose another.';
}
elseif ($found_name) {
// Name already exists
echo 'Name already exists. Please choose another.';
}
elseif ($found_email) {
// Email already exists
echo 'Email already exists. Please choose another.';
}
else {
// Insert new account
}
$stmt->close();
}
else {
// Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
echo 'Could not prepare statement!';
}
$con->close();
我想你會希望你的 SQL 語句使用這樣的JOIN
,以避免重復的行匹配:
SELECT
users.id, password
FROM
users JOIN registered
USING(id)
WHERE
username = ?
OR (nameFirst = ? AND nameLast = ?)
OR email = ?
然后,您需要將附加參數添加到綁定中:
$stmt->bind_param(
'ssss',
$_POST["username"],
$_POST["firstname"],
$_POST["lastname"],
$_POST["email]
);
$stmt->execute();
// etc.
這? 將要改變。 記住我會避免雙重和三重comilla的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.