如果存在则不检查用户

时间:2015-08-04 14:05:48

标签: php mysql

我想检查用户是否存在。

继续注册用户,即使我在里面添加了支票。回声没有给我关于如何解决这个问题的准确反馈。

编辑: 代码由于评论而编辑。它不会退出或检查现有用户。

这是我的代码:

if($_POST['username']) {
    if ( $password == $c_password ) {

        $db_name     = '*';
        $db_user     = '*';
        $db_password = '*';
        $server_url  = '*';

        $mysqli = new mysqli($server_url , $db_user, $db_password, $db_name);


        /* check connection */
        if (mysqli_connect_errno()) {
            error_log("Connect failed: " . mysqli_connect_error());
            echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}';
        } else {

            $check="SELECT * FROM USER WHERE 'Name'='". $username."'";
            $rs = mysqli_query($mysqli,$check) or die(mysqli_error($mysqli));                


             $data = mysqli_fetch_array($rs, MYSQLI_NUM);
            if($data[0] > 1) {
                echo "User Already in Exists<br/>";
                exit;
                                }

                else
            {

            $stmt = $mysqli->prepare("INSERT INTO USER (Name, Password) VALUES (?, ?)");
            $password = md5($password);
            $stmt->bind_param('ss', $username, $password);

            /* execute prepared statement */
            $stmt->execute();

            if ($stmt->error) {error_log("Error: " . $stmt->error); }

            $success = $stmt->affected_rows;

            /* close statement and connection */
            $stmt->close();

            /* close connection */
            $mysqli->close();
            error_log("Success: $success");

            if ($success > 0) {
                error_log("User '$username' created.");
                echo '{"success":1}';
            } else {
                echo '{"success":0,"error_message":"Username Exist."}';
            }}
        }
    } else {
        echo '{"success":0,"error_message":"Passwords does not match."}';
    }
} else {
    echo '{"success":0,"error_message":"Invalid Username."}';
}

0 个答案:

没有答案