Hello, I’ve just created a brand new category called Complete Scripts. In this one you will find complete solutions of applications.
The first tutorial is called how to create a membership script with user registration. I will write here a function which will do it! We will have database structure like this :
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user` varchar(255) NOT NULL,
`pass` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
After this, here is the function to create new users and insert them into database: – first, create a .php page called functions.php
<?php
function registerNewMember($user, $pass) {
$tableUsers = "users";
if(empty($user) || empty($pass)) {
exit();
}else{
$user = addslashes(mysql_real_escape_string($user));
$pass = addslashes(mysql_real_escape_string($pass));
$sql = "insert into $tableUsers values (null, '$user', '$pass')";
$rs = mysql_query($sql);
if($rs)
{
$result = "ok";
}else{
$result = "fail";
}
}
?>
Now we need to create a page with a form where users will type their desired user & pass (please note we need to include database connection file and functions one:
<?php
include('dbConnection.php');
include('functions.php');
if(isset($_POST['sb'])) {
extract($_POST);
if(registerNewMember($username, $password) == "ok")) {
print "You are now registered";
}else{
print "Not able to add new member";
}
}
?>
<form action="" method="POST">
Pickup a new username : <input type="text" name="username"><br/>
Pikcup a brand new password : <input type="password" name="password"><br/>
<input type="submit" name="sb" id="sb" value="Register me!">
</form>
PS : For database connection please remember here