php - Not checking for User if existing

980

I want to check if a user exists or not.

It keeps registering users even though I added a check inside. The echo does not give me a accurate feedback on how to fix this issue.

Edit: Code edited due to comments. It does not exit or check for the existing user.

Here is my code:

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."}';
}

People are also looking for solutions to the problem: php - CodeIgniter Load More Comment Result

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.