Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts

    Registration Form check PostgreSQL is username is already taken

    Hey I'm really new to PHP and PostgreSQP or any database in that matter. So I'm at a loss how to do this.

    I need an if statement that says.

    Code:
    If(the username user just typed in is already in database) {
      my code here
    }
    the variable the username that the user just typed in is $userNameSignup

    how would I do that with PHP for PostgreSQL?

    also how might I redirect people to a new page once they have completed the form properly?

    Thanks Shelby
    Continually Learning

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $result = pg_query('SELECT 1 FROM users WHERE LOWER(username)=\''.strtolower(pg_escape_string('.$userNameSignup.')).'\'');
    
    if (!pg_num_rows($result))
    {
        [The username doesn't exist]
    }
    else
    {
        [The username exists]
    }

    For the redirect:

    Code:
    header('Location: http://example.org/where/to/redirect');
    Last edited by MattF; 03-23-2010 at 01:55 AM.

  • #3
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by MattF View Post
    Code:
    $result = pg_query('SELECT 1 FROM users WHERE LOWER(username)=\''.strtolower(pg_escape_string('.$userNameSignup.')).'\'');
    
    if (!pg_num_rows($result))
    {
        [The username doesn't exist]
    }
    else
    {
        [The username exists]
    }
    I'm using

    Code:
    $result = pg_query('SELECT 1 FROM logins WHERE LOWER(userName)=\''.strtolower(pg_escape_string('.$userNameSignup.')).'\'');
    
    		if (!pg_num_rows($result)) {
    			
    			$userNameSignupError = "Username already exists please choose a new one";
    			$error = true;
    			
    		}
    And thats not working =[

    what did I do wrong?

    btw


    Code:
    DROP TABLE logins;
    
    CREATE TABLE logins(
    	userName VARCHAR(25) NOT NULL PRIMARY KEY,
    	password VARCHAR(25) NOT NULL,
    	firstName VARCHAR NOT NULL,
    	lastName VARCHAR NOT NULL,
    	ageDay INTEGER NOT NULL,
    	ageMonth INTEGER NOT NULL,
    	ageYear INTEGER NOT NULL,
    	email VARCHAR(255) NOT NULL,
    	createDate DATE
    );
    does that help?
    Last edited by MrEnder; 03-23-2010 at 02:12 AM.
    Continually Learning

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by MrEnder View Post
    what did I do wrong?
    You didn't pay proper attention to the code I posted. Check the bracketed comments again.

  • #5
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by MattF View Post
    You didn't pay proper attention to the code I posted. Check the bracketed comments again.
    sigh not getting it =[ I tried switching the error to the else but that didn't work either =[
    Continually Learning

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by MrEnder View Post
    sigh not getting it =[ I tried switching the error to the else but that didn't work either =[
    Note the word starting with d in the brackets:

    Code:
    if (!pg_num_rows($result))
    {
        [The username >>>>>>>doesn't<<<<<<< exist]
    }

  • #7
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by MattF View Post
    Note the word starting with d in the brackets:

    Code:
    if (!pg_num_rows($result))
    {
        [The username >>>>>>>doesn't<<<<<<< exist]
    }
    ya... but I have nothing to put in there if the name doesn't exist... I only care if the name exists... so I redid it and put the error in the else statement... and it still didnt work =[
    Continually Learning

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Try this:

    Code:
    $result = pg_query('SELECT 1 FROM logins WHERE LOWER(userName)=\''.strtolower(pg_escape_string('.$userNameSignup.')).'\'') or exit(pg_last_error());
    
    if (pg_num_rows($result))
    {
        $userNameSignupError = 'The username already exists. Please choose a different username.';
        $error = true;
    }
    Last edited by MattF; 03-23-2010 at 02:30 AM.

  • #9
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by MattF View Post
    Try this:

    Code:
    $result = pg_query('SELECT 1 FROM logins WHERE LOWER(userName)=\''.strtolower(pg_escape_string('.$userNameSignup.')).'\'') or exit(pg_last_error());
    
    if (pg_num_rows($result))
    {
        $userNameSignupError = 'The username already exists. Please choose a different username.';
        $error = true;
    }
    It still doesn't catch the error =[
    Continually Learning

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by MrEnder View Post
    It still doesn't catch the error =[
    Use this code, exactly as is:

    Code:
    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 1);
    
    print('Username: '.$userNameSignup."\n");
    
    $result = pg_query('SELECT 1 FROM logins WHERE LOWER(userName)=\''.strtolower(pg_escape_string($userNameSignup)).'\'') or exit(pg_last_error());
    
    if (pg_num_rows($result))
    {
        exit('Username already registered.');
    }

    Edit: It may have been a typo on my part.
    Last edited by MattF; 03-23-2010 at 02:53 AM.

  • #11
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    still getting Warning: pg_query() [function.pg-query]: Query failed: ERROR: duplicate key violates unique constraint "logins_pkey" in /users/intn2201/brittains/labs/lab7noscript.php on line 172

    which doesnt help me =[
    Continually Learning

  • #12
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by MrEnder View Post
    still getting Warning: pg_query() [function.pg-query]: Query failed: ERROR: duplicate key violates unique constraint "logins_pkey" in /users/intn2201/brittains/labs/lab7noscript.php on line 172

    which doesnt help me =[

    I've edited my code above slightly. There was a typo in the syntax, so try it again, as it is now.

  • #13
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    ok that worked but it removed everything else on the page =[

    ok working at editing it to make it work. I think I'll be able to...
    Last edited by MrEnder; 03-23-2010 at 03:13 AM.
    Continually Learning

  • #14
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by MrEnder View Post
    ok that worked but it removed everything else on the page =[
    Obviously. There's an exit() in there. Just replace that exit() call with your original code.

  • Users who have thanked MattF for this post:

    MrEnder (03-23-2010)

  • #15
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by MattF View Post
    Obviously. There's an exit() in there. Just replace that exit() call with your original code.
    there working 100% now ^.^
    Continually Learning


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •