...

View Full Version : Registration Form check PostgreSQL is username is already taken



MrEnder
03-23-2010, 02:41 AM
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.


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

MattF
03-23-2010, 02:52 AM
$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:



header('Location: http://example.org/where/to/redirect');

MrEnder
03-23-2010, 03:07 AM
$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



$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




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?

MattF
03-23-2010, 03:11 AM
what did I do wrong?

You didn't pay proper attention to the code I posted. Check the bracketed comments again.

MrEnder
03-23-2010, 03:18 AM
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 =[

MattF
03-23-2010, 03:20 AM
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:



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

MrEnder
03-23-2010, 03:22 AM
Note the word starting with d in the brackets:



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 =[

MattF
03-23-2010, 03:27 AM
Try this:



$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;
}

MrEnder
03-23-2010, 03:44 AM
Try this:



$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 =[

MattF
03-23-2010, 03:50 AM
It still doesn't catch the error =[

Use this code, exactly as is:



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.

MrEnder
03-23-2010, 03:53 AM
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 =[

MattF
03-23-2010, 03:55 AM
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.

MrEnder
03-23-2010, 04:11 AM
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...

MattF
03-23-2010, 04:12 AM
ok that worked but it removed everything else on the page =[

Obviously. There's an exit() in there. :D Just replace that exit() call with your original code.

MrEnder
03-23-2010, 04:15 AM
Obviously. There's an exit() in there. :D Just replace that exit() call with your original code.

there working 100% now ^.^

MattF
03-23-2010, 04:19 AM
Glad that's sorted. Apologies for my typo. :D Btw, damned fine choice of database system you've made there. :thumbsup: Good to see someone going for the better options for a change.

MrEnder
03-23-2010, 04:23 AM
now how on earth do people make web browsers remember you from one page to another 0.0... I just realized I don't know where to begin with that...


Glad that's sorted. Apologies for my typo. :D Btw, damned fine choice of database system you've made there. :thumbsup: Good to see someone going for the better options for a change.

lol and yes I did my research ^.^ ty

MattF
03-23-2010, 04:32 AM
Cookies or sessions.

http://uk2.php.net/manual/en/book.session.php
http://uk2.php.net/manual/en/function.setcookie.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum