PDA

View Full Version : PHP Question:



agent007
June 11th, 2002, 18:03
Okay, so far, I've made a simple registration script (with my pitiful PHP knowledge :p) that adds the information into a MySQL database. Here's what I put in the file (called "reg.php"):


<?php
if ($submit)
{
$dbhost = "localhost";
$dbuser = "gta3";
$dbpass = "*";
$dbname = "*";
$userlevel = "5";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
$query = "INSERT into phpSP_users values (NULL,'$user','$password','$userlevel')";
$result = mysql_query($query);
echo "Registered successfully!";
}
else {
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="Mike Schroeder">
<meta name="generator" content="AceHTML 5 Pro">
</head>
<body>
<form action="<? echo $PHP_SELF; ?>" method="post">
<table summary="" border="0">
<tr>
<td>Username: </td>
<td><input type="text" name="user" size="40" maxlength="256"><br></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="password" size="40" maxlength="256"><br></td>
</tr>
</table>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
<?

}
?>

Now, my question is, how do I make the script check the database to make sure no other user has the same username, and if someone does, outputs an error?

For example, if joe tried to register with the username joe and someone already took that name, it would show an error (such as "Sorry, this username has been taken" or something similar).

Christopher
June 11th, 2002, 18:16
Something like this, remember to fill in the correct colum names...


<?php
// connect stuff...

$count = mysql_query("SELECT COUNT(*) FROM <<your table>> WHERE <<username colum>> = '$user'");

if($count >= 0)
{
print "Sorry, username already taken.";
}
?>

Sorry, I'm a bit rusy on my PHP, but you could try this to keep yourself busy until a more knowlegable person comes along. :)


EDIT: Also, you might want to reconsider putting in your password and stuff ;)

agent007
June 11th, 2002, 18:27
Originally posted by Christopher
Something like this, remember to fill in the correct colum names...


<?php
// connect stuff...

$count = mysql_query("SELECT COUNT(*) FROM <<your table>> WHERE <<username colum>> = '$user'");

if($count >= 0)
{
print "Sorry, username already taken.";
}
?>

Sorry, I'm a bit rusy on my PHP, but you could try this to keep yourself busy until a more knowlegable person comes along. :)


EDIT: Also, you might want to reconsider putting in your password and stuff ;)

Thanks for the code :)

Bah, I just copied it directly - didn't even notice it. :p

Christopher
June 11th, 2002, 18:39
Well I'm curious to see if it works, I doubt it will :D

And I keep spelling "column" wrong, gees, gotta think more before posting!

agent007
June 11th, 2002, 20:35
Originally posted by Christopher
Well I'm curious to see if it works, I doubt it will :D

Bah, you're right. It says the user has been added successfully, but when the newly made duplicate user tries to login, it says the login fails. That part is okay, but what bothers me is that it says the user has been added successfully. :(

agent007
June 11th, 2002, 20:42
Whoo! Figured it out. :)

I had to put this inside the file:


$UsernameQuery = mysql_query ("SELECT * FROM phpSP_users WHERE user='$user'");
$UsernameExist = mysql_num_rows($UsernameQuery);
if ($UsernameExist>0)
{
print "<b>Username already exists in the database!</b>";
exit;
}


It works perfectly! :D

Christopher
June 12th, 2002, 07:51
I was playing around with code from my own site last night, the SELECT COUNT(*) doesn't work as it sounds... I printed it to the screen expecting a numbert and it turned out to be "Resource id #3" so I just used mysql_num_rows too :D

(And in the code I gave you I put equal or greater than 0, returning true no matter what, in this case.)