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).
<?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).