Add a Member to a Users Database (Rated 5)Description:
This is a simple example of how you can add users to a database to allow them to register to see various area's of your site.
I suggest having some javascript client side validation of the text fields, along with server side validation in case people don't have java turned on. Code starts here
// SQL to create the Users Table
CREATE TABLE users (
UserID int(11) NOT NULL auto_increment,
UserName text NOT NULL,
Password text NOT NULL,
PRIMARY KEY (UserID)
) TYPE=MyISAM;
// Registration form.html
<form id="registration" name="registration" method="POST" action="register.php">
<table width="100%" border="0" align="center" cellpadding="8" cellspacing="0">
<tr>
<td><div class="small" align="right">Username *</div></td>
<td><input name="username" type="text" size="30" maxlength="35" /></td>
</tr>
<tr>
<td><div class="small" align="right">Password *</div></td>
<td><input name="password" type="password" size="30" maxlength="35" /></td>
</tr>
<tr>
<td><div class="small" align="right">* denotes <strong>required</strong> fields</div></td>
<td>
<input type="submit" name="submit" value="Register" onclick="" />
</td>
</tr>
</table>
</form>
// register.php
<?PHP
include_once('form.html');
if(isset($_POST['submit']))
{
$username = strip_tags($_POST['username']) ;
$password = md5($_POST['password']);
// Connect to your database here...
//Check details not already in database..
$q1 = "SELECT COUNT(*) FROM `users` WHERE `UserName` = '$username'";
$res = mysql_query($q1);
$res = mysql_fetch_array($res);
if ($res[0] == 0)
{
// Users is a simple table with UserID (auto inc), password and username.
$query = "INSERT INTO `users` (`UserID`, `Password`, `UserName`) VALUES ('','$password', '$username')";
$result = mysql_query($query) or die ("Error executing " . $query . " - " . mysql_error());
}
else
{
// Username or email address already in use
// Handle this how you want
}
}
?>
Last Edited: March 20, 2003, 12:37 pm
Submitted by Devscripts on 20-03-2003 12:37 |