[英]My Profile page, UPDATE details PHP MYSQL, verify uniqueness logic
我有一个html表单,其中用户名和电子邮件字段在注册时应该是唯一的。 我已经成功完成了此注册页面,它验证了用户名和电子邮件的唯一性,并检查了密码强度等。
然后,我创建了“我的个人资料”页面,该页面只有登录后才能访问,我应该在该页面上让用户更新其详细信息,甚至包括电子邮件和用户名。 我最初是通过读取用户数据来填写此表格的,
<input type="text" name="username" value="<?php echo $user_data['username']?>"/>
麻生
显然,用于注册的规则在更新帐户详细信息时也应保持一致。 因此,我使用相同的代码来检查这些字段。 我遇到了一个问题。
假设我们有用户名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.';
}
}
首先:不要让用户将键字段修改为用户名或密码...如果数据库增长并且其他表通过用户名或电子邮件之类的键字段与用户表相关,则会遇到很多问题。 现在让我们面对问题。您应该一起检查电子邮件和用户名的唯一性,而不是分别检查它们。 因此,“ user_exists”函数应获取两个参数:用户名和电子邮件,并检查是否存在具有给定电子邮件和密码的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.