0

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."}';
}
Community
  • 1
  • 1
swift_dan
  • 2,628
  • 2
  • 12
  • 19
  • tip: `$rs = mysqli_query($mysqli,$check) or die(mysqli_error($mysqli));` then see the link used to close this as a duplicate. The answers are in there as it will point out in the error you will receive. Plus make sure `$username` is indeed assigned somewhere. – Funk Forty Niner Aug 04 '15 at 14:08
  • thank you for your tip and comment. the app runs through without an error. $username is set and inserted into the table. – swift_dan Aug 04 '15 at 14:14
  • you're welcome. So... everything's running hunky dory then. – Funk Forty Niner Aug 04 '15 at 14:15
  • not really. he should check for user or die ;) but is not dead at the end...he keeps inserting new users called "dan" – swift_dan Aug 04 '15 at 14:21
  • Ok. well, update your question with the new code you are using and marked as an additional and marked edit. I will then reopen the question where I or possibly others offer a solution since the closed question may not be addressing everything. You should add an `exit;` after `echo "User Already in Exists
    ";` though, because you code may want to continue to execute here, which I'm under the impression is doing just that.
    – Funk Forty Niner Aug 04 '15 at 14:27
  • just edited. tried your solution but it keeps adding dans ;) this name must be really prominent... – swift_dan Aug 04 '15 at 14:35
  • you still haven't addressed this `SELECT * FROM USER WHERE 'Name'` you're using regular single quotes around `Name`. Either remove those, or use ticks `\`` which resemble quotes but are not the same. That should have given you a syntax error but you're not sharing that. – Funk Forty Niner Aug 04 '15 at 14:36
  • still success with the ticks ` `Name`=`dan` – swift_dan Aug 04 '15 at 14:50
  • I reopened your question, but I for one won't be able to help any further. Good luck. – Funk Forty Niner Aug 04 '15 at 14:54

0 Answers0