5 Replies - 868 Views - Last Post: 04 December 2012 - 04:02 PM

#1 itsjimmy91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 19-January 11

PHP Check to see if User exists

Posted 04 December 2012 - 03:49 PM

Hey guys. I have a simple registration form and I'm trying to put together some PHP code that checks the database to see if the username or email already exists, and if it does, then it will not go through with adding the new user to the database and it will send the user back to the registration page with the data still in the form and an error.

I have this code, and it does work for putting users into the database. The problem is, it is inserting them every time, even if I enter a username or an email that already exists, and I'm not sure why. I'm working with MySQL.

This is what I have so far:

$email = $_POST["email"];
    $username = $_POST["username"];
    
    $emailquery = mysql_query("SELECT * FROM users WHERE email='$email'");
    $usernamequery = mysql_query("SELECT * FROM users WHERE username='$username'");
    
    
    if(mysql_num_rows($emailquery) != 0)
    {
        echo "Email address already exists";
        // redirect
        $url = 'register.php';
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    }
    
    else if(mysql_num_rows($usernamequery) != 0)
    {
        echo "User name already exists";
        // redirect
        $url = 'register.php';
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    }
    
    else
    {
        $sql="INSERT INTO Users (FirstName, LastName, Email, UserName, Password)
        VALUES
        ('$_POST[firstname]','$_POST[lastname]','$_POST[email]', '$_POST[username]', '$_POST[password]')";
    
        if (!mysql_query($sql,$con))
        {
          die('Error: ' . mysql_error());
        }
    
        $url = 'home.php';
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    
         mysql_close($con);
    }



Oops, I meant to post this in the PHP forum. Could a mod please move this?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Check to see if User exists

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3509
  • View blog
  • Posts: 10,132
  • Joined: 08-June 10

Re: PHP Check to see if User exists

Posted 04 December 2012 - 03:50 PM

the most simple solution were to have a UNIQUE (or PRIMARY KEY) constraint on the username and email fields in the database. if you then try to insert an already existing name or email, the DB will return with a constraint violation error where you then can tell the user, that there were duplicate values submitted.
Was This Post Helpful? 1
  • +
  • -

#3 itsjimmy91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 19-January 11

Re: PHP Check to see if User exists

Posted 04 December 2012 - 03:52 PM

View PostDormilich, on 04 December 2012 - 03:50 PM, said:

the most simple solution were to have a UNIQUE (or PRIMARY KEY) constraint on the username and email fields in the database. if you then try to insert an already existing name or email, the DB will return with a constraint violation error where you then can tell the user, that there were duplicate values submitted.


ah, ok. That would be MUCH easier... I'll do that. What will actually happen if someone does enter a name or password that's already in the database? Will I have to code something to display the error and redirect to the form with the data already entered, or does it do it itself?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3509
  • View blog
  • Posts: 10,132
  • Joined: 08-June 10

Re: PHP Check to see if User exists

Posted 04 December 2012 - 03:54 PM

of course it doesn’t do it itself. you’d have to code something that the user notifies of his mistake.
Was This Post Helpful? 0
  • +
  • -

#5 itsjimmy91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 19-January 11

Re: PHP Check to see if User exists

Posted 04 December 2012 - 04:00 PM

Sorry, new to PHP. How would I send a user back to the page with the values that were already entered?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3509
  • View blog
  • Posts: 10,132
  • Joined: 08-June 10

Re: PHP Check to see if User exists

Posted 04 December 2012 - 04:02 PM

you don’t "send him back". rather you take the form and fill it with the passed values (and of course the error notification) and send that back.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1