PHP提交问题

时间:2010-04-06 20:59:19

标签: php mysql

我正在尝试检查username是否可用并显示给用户以查看他们何时检查帐户设置,我已经完成了。

但是当用户尝试填写另一个字段时,我会收到Your username is unavailable!,因为它已经是用户username而不应该弹出。我想知道如何使用PHP修复此问题,以便每次用户查看其帐户设置时都会显示用户名,并且在用户提交其他信息时不会出现问题?

这是PHP代码。

if (isset($_POST['submitted'])) {

    require_once '../htmlpurifier/library/HTMLPurifier.auto.php';

    $config = HTMLPurifier_Config::createDefault();
    $config->set('Core.Encoding', 'UTF-8');
    $config->set('HTML.Doctype', 'XHTML 1.0 Strict');
    $config->set('HTML.TidyLevel', 'heavy');
    $config->set('HTML.SafeObject', true);
    $config->set('HTML.SafeEmbed', true);
    $purifier = new HTMLPurifier($config);

    $mysqli = mysqli_connect("localhost", "root", "", "sitename");
    $dbc = mysqli_query($mysqli,"SELECT users.*
                                 FROM users 
                                 WHERE user_id=3");

    $first_name = mysqli_real_escape_string($mysqli, $purifier->purify(htmlentities(strip_tags($_POST['first_name']))));
    $username = mysqli_real_escape_string($mysqli, $purifier->purify(htmlentities(strip_tags($_POST['username']))));


    if($_POST['username']) {
        $u = "SELECT user_id 
              FROM users 
              WHERE username  = '$username'";
        $r = mysqli_query ($mysqli, $u) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($mysqli));

        if (mysqli_num_rows($r) == TRUE) {
            $username = NULL;
            echo '<p class="error">Your username is unavailable!</p>';
        } else if(mysqli_num_rows($r) == 0) {
            $username = mysqli_real_escape_string($mysqli, $purifier->purify(htmlentities(strip_tags($_POST['username']))));



    if ($_POST['password1'] == $_POST['password2']) {
        $sha512 = hash('sha512', $_POST['password1']);
        $password = mysqli_real_escape_string($mysqli, $purifier->purify(strip_tags($sha512)));
    } else {
        $password = NULL;
    } 

    if($password == NULL) {
        echo '<p class="error">Your password did not match the confirmed password!</p>';
    } else {


        if (mysqli_num_rows($dbc) == 0) {
                $mysqli = mysqli_connect("localhost", "root", "", "sitename");
                $dbc = mysqli_query($mysqli,"INSERT INTO users (user_id, first_name, username, password) 
                                             VALUES ('$user_id', '$first_name', '$username', '$password')");
        }


        if ($dbc == TRUE) {
                $dbc = mysqli_query($mysqli,"UPDATE users 
                                             SET first_name = '$first_name', username  = '$username', password = '$password' 
                                             WHERE user_id = '$user_id'");

                echo '<p class="changes-saved">Your changes have been saved!</p>';

        }

        if (!$dbc) {
                print mysqli_error($mysqli);
                return;
        }

    }

        }

    }
}

这是html表单。

<form method="post" action="index.php">
    <fieldset>
        <ul>
            <li><label for="first_name">First Name: </label><input type="text" name="first_name" id="first_name" size="25" class="input-size" value="<?php if (isset($_POST['first_name'])) { echo  stripslashes(htmlentities(strip_tags($_POST['first_name']))); } else if(!empty($first_name)) { echo  stripslashes(htmlentities(strip_tags($first_name))); } ?>" /></li>
            <li><label for="username">UserName: </label><input type="text" name="username" id="username" size="25" class="input-size" value="<?php if (isset($_POST['username'])) { echo  stripslashes(htmlentities(strip_tags($_POST['username']))); } else if(!empty($username)) { echo  stripslashes(htmlentities(strip_tags($username))); } ?>" /><br /><span>(ex: CSSKing, butterball)</span></li>
            <li><label for="password1">Password: </label><input type="password" name="password1" id="password1" size="25" class="input-size" value="<?php if (isset($_POST['password1'])) { echo  stripslashes(htmlentities(strip_tags($_POST['password1']))); } ?>" /></li>
            <li><label for="password2">Confirm Password: </label><input type="password" name="password2" id="password2" size="25" class="input-size" value="<?php if (isset($_POST['password2'])) { echo  stripslashes(htmlentities(strip_tags($_POST['password2']))); } ?>" /></li>

            <li><input type="submit" name="submit" value="Save Changes" class="save-button" />
                <input type="hidden" name="submitted" value="true" />
            <input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li>
            </ul>
    </fieldset>

</form>

1 个答案:

答案 0 :(得分:1)

当您在这些行上检查提交的表单时:

if($_POST['username']) {
    $u = "SELECT user_id 
          FROM users 
          WHERE username  = '$username'";

您应该设置用户的ID以防止被锁定在同一记录中:

    $u = "SELECT user_id 
          FROM users 
          WHERE username  = '$username'
          AND user_id <> 3";

那是因为需要在所有其他用户的用户名字段上进行检查,排除当前用户:)

希望这有帮助!