Help With Signup
- dburford11
-
dburford11
- Member since: Apr. 1, 2005
- Offline.
-
- Forum Stats
- Member
- Level 03
- Programmer
Can anyone tell me how to make this script to make sure the username isn't already taken?
<?
include 'conn.php';
/* Declare Variables */
$user = $_POST['user'];
$pass = $_POST['pass'];
/* Chack if Variables Are Filled */
if(!$user) {
echo 'Username is Empty';
} else if(!$pass) {
echo 'Password is Empty';
} else if(!$user && !$pass) {
echo 'All Feilds Are Blank';
} else {
/* Check if username already exists */
$result=mysql_query("SELECT user FROM users WHERE user='$user'");
if(mysql_num_rows($result)==1){
die("The username $user is already in use.");
} else {
mysql_query("INSERT INTO users (user, pass) VALUES ('$user', '$pass')");
echo 'Username ' . $user . ' Added';
}
}
?>
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
That would be what these lines do...
$result=mysql_query("SELECT user FROM users WHERE user='$user'");
if(mysql_num_rows($result)==1){
die("The username $user is already in use.");
It should work perfectly although using mysql count would be the better way, that's fine.
- dburford11
-
dburford11
- Member since: Apr. 1, 2005
- Offline.
-
- Forum Stats
- Member
- Level 03
- Programmer
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
At 11/6/06 09:06 PM, dburford11 wrote: but it doesn't work
k, try replacing that with this:
$sql = 'SELECT COUNT(*) ';
$sql .= 'FROM `users` ';
$sql .= "WHERE user = '$user'";
$result=mysql_query($sql);
$count=mysql_result($result,0,"count(*)");
if ($count != 0)
{
echo 'That username is already taken.';
exit;
}else{
// do the insert...
}
- dburford11
-
dburford11
- Member since: Apr. 1, 2005
- Offline.
-
- Forum Stats
- Member
- Level 03
- Programmer
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
At 11/6/06 09:16 PM, dburford11 wrote: that doesn't work either
What do you mean it doesn't work?
Does it give an error, does it allow no usernames to go in, does it not check if it's taken?
Check your field and table names, because the code's fine.
- dburford11
-
dburford11
- Member since: Apr. 1, 2005
- Offline.
-
- Forum Stats
- Member
- Level 03
- Programmer
it doesn't check if the username doesn't exist
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
At 11/6/06 09:22 PM, dburford11 wrote: it doesn't check if the username doesn't exist
Did you check the table and field names?
Also, post the full script that you have now.


