2
我創建了一個用戶登錄系統並擁有一個用戶可以更新其詳細信息的頁面。電子郵件地址和用戶名必須是唯一的,因此我在更新表單中添加了驗證。但是,表單提交時出現錯誤,說明電子郵件地址已被使用。 我能以某種方式從重複檢查中排除當前電子郵件(存儲在會話中)嗎?檢查重複的電子郵件地址,但排除當前用戶
這是我的代碼,但不是所有的,因爲它是有點長,我也需要做用戶名相同的驗證檢查:
try {
$stmt = $db->prepare('SELECT email, username, firstname, middlename, lastname FROM members WHERE memberID = :memberID');
$stmt->execute(array(':memberID' => $_SESSION['memberID']));
$row = $stmt->fetch();
} catch(PDOException $e) {
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
}
//process form
if(isset($_POST['submit'])){
// email validation
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$error[] = 'Please enter a valid email address';
} else {
$stmt = $db->prepare('SELECT email FROM members WHERE email = :email');
$stmt->execute(array(':email' => $_POST['email']));
$emailcheck = $stmt->fetch(PDO::FETCH_ASSOC);
if(!empty($emailcheck['email'])){
$error[] = 'Email provided is already in use.';
}
}
// insert to DB
if(!isset($error)){
try {
$stmt = $db->prepare('UPDATE members SET username=:username, firstname=:firstname, middlename=:middlename, lastname=:lastname, email=:email, updated=NOW() WHERE memberID = :memberID');
$stmt->execute(array(
':username' => $_POST['username'],
':firstname' => $_POST['firstname'],
':middlename' => $_POST['middlename'],
':lastname' => $_POST['lastname'],
':email' => $_POST['email'],
':memberID' => $_SESSION['memberID']
));
//redirect to page with updated message
header('Location: personal-details.php?action=updated');
exit;
} catch(PDOException $e) {
$error[] = $e->getMessage();
}
} else {
$error[] = 'Update error';
}
}
然後,在我使用的每個文本框:
<input type="text" name="username" id="username" class="form-control" value="<?php echo($row['username']) ?>" tabindex="1">
你牛逼關於**用戶登錄**或**用戶註冊**頁面?在用戶登錄中,用戶名必須存在。在用戶註冊中,用戶名不得存在(對於電子郵件來說,也是如此)。 – FDavidov