php - 我的个人资料页面,更新详细信息 PHP MYSQL,验证唯一性逻辑

标签 php mysql post sql-update logic

我有一个 html 表单,其中字段用户名和电子邮件在注册时应该是唯一的。我已成功制作此注册页面,它验证用户名和电子邮件的唯一性,检查密码强度等。

然后,我创建了一个“我的个人资料”页面,只有登录后才能访问,我应该让用户更新他们的详细信息,甚至电子邮件和用户名。我最初通过读取用户数据来填写此表单,如下所示

<input type="text" name="username" value="<?php echo $user_data['username']?>"/> 

a.s.o

用于注册的规则显然在更新帐户详细信息时也应保持一致。因此我使用相同的代码来检查这些字段。不过我遇到了问题。

假设我们有用户名 Billy 和 Billy2。 Billy 无法更新到 Billy2,因为 Billy2 已存在。完美的。 但比利无法更新为比利,因为比利也已经存在。

注释的 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';
?>

最佳答案

将每组用户名电子邮件检查都包含在其是否已更改的测试中,而不是将所有检查包装在单个测试中以判断其中任何一个是否已更改。

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/

相关文章:

mysql - 无法使用外键更改表

Django POST 子字典

php - Yii - setPathOfAlias 中的 getPathOfAlias

php - Rel 属性在 mysql 数据库中不起作用

php - <table> 结束 <div> 标签

http - 去urlfetch.Transport.RoundTrip,GET可以调用,POST不行?

http - 多部分/表单数据与应用程序/八位字节流

php - 为什么对已经存在的标准函数使用自定义 sql 函数?

mysql - installshield 是否适合这种情况?

"or"语句中的 MySQL 索引