我有一个html表单,其中用户名和电子邮件字段在注册时应该是唯一的。我已经成功完成了此注册页面,它验证了用户名和电子邮件的唯一性,并检查了密码强度等。
然后,我创建了“我的个人资料”页面,该页面仅在登录后才能访问,我应该让用户更新其详细信息,甚至包括电子邮件和用户名。我最初是通过读取用户数据来填写此表格的,
<input type="text" name="username" value="<?php echo $user_data['username']?>"/>
a.s.o
显然,用于注册的规则在更新帐户详细信息时也应保持一致。因此,我使用相同的代码来检查这些字段。我遇到了一个问题。
假设我们有用户名Billy和Billy2。 Billy无法更新到Billy2,因为Billy2已经存在。完善。
但是Billy无法更新到Billy,因为Billy也已经存在。
评论的if是我试图解决问题的尝试,尽管这使我可以将Billy保留为Billy,但同时也降低了唯一性,它使我可以将Billy更新为Billy2,这并不酷,因为现在有两个Billy2。
所以这是myprofile.php的完整代码
您将如何解决?
<?php
include 'core/init.php';
protect_page();
if (empty($_POST) === false) {
$required_fields = array('username', 'password', 'password_again', 'first_name', 'last_name' 'email');
foreach($_POST AS $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true) {
$errors[] = 'Fields marked with an asterisk are required.';
break 1;
}
}
if (empty($errors) === true) {
if (strlen($_POST['password']) < 6) {
$errors[] = 'Your password must be at least 6 characters long.';
} elseif (($_POST['password']) !== ($_POST['password_again'])) {
$errors[] = "Sorry, new password doesn't match. Please try again.";
}
if ($_POST['password'] !== $_POST['password_again']) {
$errors[] = 'Passwords don\'t match.';
}
// if ($user_data['username'] !== $_POST['username'] && $user_data['email'] !== $_POST['email']) {
if (user_exists($_POST['username']) === true) {
$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is already taken.';
}
if (preg_match("/\\s/", $_POST['username']) === true) {
$errors[] = 'Your username must not contain any spaces.';
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'A valid email address must be provided.';
}
if (email_exists($_POST['email']) === true){
$errors[] = 'Sorry, the email address \'' . $_POST['email'] . '\' is already in use.';
}
}
// }
}
// print_r($errors);
include'includes/overall/header.php';
?>
<h1>My Profile</h1>
<br>
<p>Here you can change your account details or <br><a href="index.php">Cancel and return</a> to the Homepage.</p>
<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
echo 'Your details have been succesfully updated.';
} else {
if (empty($_POST) === false && empty($errors) === true) {
change_password($session_user_id, $_POST['password'], $_POST['username'], $_POST['first_name'], $_POST['last_name'], $_POST['email']);
Header('Location: changedetails.php?success');
} elseif (empty($errors) === false) {
echo output_errors($errors);
}
?>
<form action="" method="post">
<ul>
<li>Username:*<br>
<input type="text" name="username" value="<?php echo $user_data['username']?>"/>
</li><br>
<li>First Name:*<br>
<input type="text" name="first_name" value="<?php echo $user_data['first_name']?>"/>
</li><br>
<li>Last Name:*<br>
<input type="text" name="last_name" value="<?php echo $user_data['last_name']?>"/>
</li><br>
<li>Email:*<br>
<input type="text" name="email" value="<?php echo $user_data['email']?>"/>
</li><br>
<br><h2>Change Password:</h2>
<li>
New Password*:<br>
<input type="password" name="password" />
</li>
<li>
Confirm New Password*:<br>
<input type="password" name="password_again" />
</li>
<li>
<input type="submit" value="Update Profile">
</li>
</ul>
<a href="index.php">Cancel</a>
<?php
}
include'includes/overall/footer.php';
?>
最佳答案
用测试是否包装了username
和email
的每组检查,而不是将所有检查包装在单个测试中,检查其中是否已更改。
if ($user_data['username'] !== $_POST['username'] {
if (user_exists($_POST['username']) {
$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is already taken.';
}
if (preg_match("/\\s/", $_POST['username']) === true) {
$errors[] = 'Your username must not contain any spaces.';
}
}
if ($user_data['email'] != $_POST['email']) {
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'A valid email address must be provided.';
}
if (email_exists($_POST['email']) === true){
$errors[] = 'Sorry, the email address \'' . $_POST['email'] . '\' is already in use.';
}
}
关于php - 我的个人资料页面,更新详细说明了PHP MYSQL,验证了唯一性逻辑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34421444/