简体   繁体   中英

PHP to mySQL check if user exists

I have a script that updates/creates user from an iOS device. Now i want to have the script also check if the user already exists in the database. I am going to restrict this to username for now, so no more than ONE unique username may exist. I have an if-statement in my PHP but i cannot get it to work - help please :).

    <?php

    header('Content-type: application/json');
    if($_POST) {
        $username   = $_POST['username'];
        $password   = $_POST['password'];

        if($username && $password) {

                $db_name     = 'dbname';
                $db_user     = 'dbuser';
                $db_password = 'dbpass';
                $server_url  = 'localhost';

                $mysqli = new mysqli('localhost', $db_user, $db_password, $db_name);

            $userexists = mysql_query("SELECT * FROM users WHERE username='$username'"); 

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

            if(mysql_num_rows($userexists) != 0) { 
                echo '{"success":0,"error_message":"Username Exist."}';
            } 

            else {

                $stmt = $mysqli->prepare("INSERT INTO users (username, password, email) VALUES (?, ?, ?)");
                $password = md5($password);

                $stmt->bind_param('sss', $username, $password, $email);

                    /* 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."}';
    }
}   
else {
    echo '{"success":0,"error_message":"Invalid Data."}';
}
?>

You could SELECT the table before trying to insert username . If it already exists (= you have a result) you dont simply insert.

Better yet, use ON DUPLICATE IGNORE or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM