Oke, in this tutorial I'm going to explain how to make a registration system.
The register system
Oke, let's start with the database, we'll need a database where we'll put all accounts into. Import this SQL:
Quote:
CREATE TABLE `register` (
`id` int(5) NOT NULL auto_increment,
`username` varchar(40) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(35) NOT NULL,
`country` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
|
just put it into a .sql file and import it, or just goto 'SQL' and copy-past it. This will create the table register, were all account information after registrating, will be putting into. Let's start with making a form for the registration system. We let the user fill in the wanted username, password, email and country.
Quote:
<h4><b>REGISTER</h4></b>
<form method='POST' action='register.php'>
<b>username:</b> <input type='text' name='username'> <br>
<b>password:</b> <input type='password' name='password'> <br>
<b>confirm password:</b> <input type='password' name='password_confirm'> <br>
<b>email:</b> <input type='text' name='email'> <br>
<b>country:</b> <input type='text' name='country'> <p>
<input type='submit' value='register' name='submit'>
</form>
|
Easy, isn't it? Once the submit button is pressed, it will set all fields, filled in values, into $_POST['field_name']. We just let the user fill in the user info he wants, but now we have to check if the info he filled in is 'ok' and if he filled in everything correctly. We'll also check if the username not just already does exist.
Quote:
mysql_connect(localhost, root, password);
mysql_select_db(scripts);
if(isset($_POST['submit'])) {
if(!isset($_POST['username']) || empty($_POST['username']) || !isset($_POST['password']) || empty($_POST['password']) || !isset($_POST['email']) || empty($_POST['email']) || !isset($_POST['country']) || empty($_POST['country'])) {
echo "<font color='red'>Please fill in all fields!</font>";
}else{
if($_POST['password'] != $_POST['password_confirm']) {
echo "<font color='red'>Please confirm your password correctly!</font>";
}else{
$query = mysql_query("INSERT INTO register(username, password, email, country) VALUES ('".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '".$_POST['country']."') ") or die("<font color='red'>Could not create your account, please contact the site web master!</font>");
echo "<font color='green'>You have succesfully been registrated! </font>";
}
}
}else{
//form here
}
|
oke, this is the full script which will check what is filled in, and if everything is correctly: it will insert it into the database (create the account). Oke, lets go trough the script.
Quote:
mysql_connect(localhost, root, password);
mysql_select_db(scripts);
|
this will connect to localhost, with username: root, en password: password, on database: scripts, change this to whatever login info you have on your (local)host.
Quote:
|
if(isset($_POST['submit'])) {
|
this part will check if the $_POST['submit'] does exist with the function:
isset() if the var $_POST['submit'] DOES exist, the submit button have been pressed(caus once the user press on the button called 'submit', all input fields will be set into $_POST['field_name']).
Quote:
|
if(!isset($_POST['username']) || empty($_POST['username']) || !isset($_POST['password']) || empty($_POST['password']) || !isset($_POST['email']) || empty($_POST['email']) || !isset($_POST['country']) || empty($_POST['country'])) {
|
this part will check if the user didn't leave any fields empty, if he did, there will be an error message(by the follow script):
echo "<font color='red'>Please fill in all fields!</font>";
Quote:
}else{
if($_POST['password'] != $_POST['password_confirm']) {
echo "<font color='red'>Please confirm your password correctly!</font>";
|
but if everything is filled in(}else{) then it will check if the password is correctly confirmed, if the 2 passwords filled in, doesn't match, it will give again an error.
Quote:
}else{
$query = mysql_query("INSERT INTO register(username, password, email, country) VALUES ('".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '".$_POST['country']."') ") or die("<font color='red'>Could not create your account, please contact the site web master!</font>");
echo "<font color='green'>You have succesfully been registrated! </font>";
|
but if everything is filled in correctly, then it will insert everything into the database, the table: register(fields: username, password, email and country) and the values that will be put into those fields will be the values that are filled in in the form.
Now let's put all together.
Quote:
<?php
mysql_connect(localhost, root, password);
mysql_select_db(scripts);
if(isset($_POST['submit'])) {
if(!isset($_POST['username']) || empty($_POST['username']) || !isset($_POST['password']) || empty($_POST['password']) || !isset($_POST['email']) || empty($_POST['email']) || !isset($_POST['country']) || empty($_POST['country'])) {
echo "<font color='red'>Please fill in all fields!</font>";
}else{
if($_POST['password'] != $_POST['password_confirm']) {
echo "<font color='red'>Please confirm your password correctly!</font>";
}else{
$query = mysql_query("INSERT INTO register(username, password, email, country) VALUES ('".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '".$_POST['country']."') ") or die("<font color='red'>Could not create your account, please contact the site web master!</font>");
echo "<font color='green'>You have succesfully been registrated! </font>";
}
}
}else{
?>
<h4><b>REGISTER</h4></b>
<form method='POST' action='testje.php'>
<b>username:</b> <input type='text' name='username'> <br>
<b>password:</b> <input type='password' name='password'> <br>
<b>confirm password:</b> <input type='password' name='password_confirm'> <br>
<b>email:</b> <input type='text' name='email'> <br>
<b>country:</b> <input type='text' name='country'> <p>
<input type='submit' value='register' name='submit'>
</form>
<?php
}
?>
|
and now we have our own registration script!
I hope you understanded it, caus it's hard for me to explain it in good English...
Well, anyways, enjoy ^^.