What Will I Learn?
- You will learn how to create a registration form
- You will learn how to make form structure
- You will learn how to create login form
- you will learn how to connect PHP form to MySQL database
Requirements
- you must know basic of PHP programming
- you must understand SQL query
- you must know how to implement SQL query on PHP file
Difficulty
- Intermediate
Tutorial Contents
almost all websites use form regestration, form registration serves to require visitors to register when first accessing a web. for example facebook, instagram, gmail, and others. you must imagine that it is very difficult to create a registration form. actually not too hard, you just have to understand about PHP programming, and MySQL database.
- the first step you should do is create a database as the place where user data will be stored.
we make it through localhost / phpmyadmin, then fill in the name of the database you want to create, then press submit button
- then we will add a table in our database, how to type the script below into the SQL column.
CREATE TABLE IF NOT EXISTS `user` (
`id_user` int(2) NOT NULL AUTO_INCREMENT,
`userid` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`aktif` enum('Y','N') NOT NULL DEFAULT 'Y',
`nama` varchar(50) NOT NULL,
`alamat` varchar(100) NOT NULL,
`email` varchar(30) NOT NULL,
`hp` varchar(20) NOT NULL,
PRIMARY KEY (`id_user`)
)
- then we will create a form, type the script below then save with name registration.php
<form name="form1" method="post" action="registration.php?act=simpan">
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<tr valign="middle" bgcolor="#78hh99">
<td height="40" colspan="2"><div align="center" class="style1 style2">REGISTRATION FORM </div></td>
</tr>
<tr bgcolor="#CCCCCC">
<td><div align="right">Name</div></td>
<td>:
<input name="nama" type="text" id="nama"></td>
</tr>
<tr bgcolor="#CCCCCC">
<td><div align="right">Addres</div></td>
<td>:
<textarea name="alamat" id="alamat"></textarea></td>
</tr>
<tr bgcolor="#CCCCCC">
<td><div align="right">Pos Code </div></td>
<td>:
<input name="kpos" type="text" id="kpos"></td>
</tr>
<tr bgcolor="#CCCCCC">
<td><div align="right">Gender </div></td>
<td>:
<select name="jk" id="jk">
<option value="Laki-Laki">men</option>
<option value="perempuan" selected>women</option>
</select></td>
</tr>
<tr bgcolor="#CCCCCC">
<td><div align="right">born place </div></td>
<td>:
<input name="tempatlahir" type="text" id="email3"></td>
</tr>
<tr bgcolor="#CCCCCC">
<td><div align="right">birthday </div></td>
<td>:
<input name="tanggallahir" type="text" id="email4">
*dd/mm/yyyy</td>
</tr>
<tr bgcolor="#CCCCCC">
<td><div align="right">email/username</div></td>
<td>:
<input name="email" type="text" id="email"></td>
</tr>
<tr bgcolor="#CCCCCC">
<td><div align="right">phone number</div></td>
<td>:
<input name="hp" type="text" id="hp"></td>
</tr>
<tr bgcolor="#CCCCCC">
<td><div align="right">Password </div></td>
<td>:
<input name="password" type="password" id="password"></td>
</tr>
<tr bgcolor="#CCCCCC">
<td> </td>
<td><input type="submit" name="Submit" value="Register">
<input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
- the script above is the design of the form we want to create.
- then we will create a script insert that serves to enter data from form to database, add the script below into the registrtion.php script at the top.
<?php
if ($_GET['act']=="simpan") {
include "connect.php";
$cek_user=mysql_num_rows(mysql_query("SELECT * FROM user WHERE userid='$_POST[userid]'"));
if ($cek_user > 0) {
echo '
alert ("User already exist");
window.location="index.php";
';
exit();
}
else {
$level='user';
$password= $_POST["password"];
mysql_query("INSERT INTO user (nama, alamat, kodepos, email, jk,telpon,tmpt_lahir,tgl_lahir,level,password)
Values ('$_POST[nama]','$_POST[alamat]','$_POST[kpos]','$_POST[jk]','$_POST[email]','$_POST[hp]','$_POST[tempatlahir]','$_POST[tanggallahir]','$level','$password')");
echo '
alert ("Registration Succes");
window.location="registration.php";
';
exit();
}
}
?>
- okay, then we will make a connection between PHP form and MySQL database.
- type the script below and save it with the name connect.php
<?php
$server="localhost";$user="root";$password="";$database="db_user";
mysql_connect($server,$user,$password)or die ("not connect");
mysql_select_db($database) or die ("failed to connect");
- okay file is complete, then we try to run through registration.php file, but before we have to run localhost APACHE and MySQL. as shown below.
- then run the registration.php file, type the file name in your browser search field as shown below.
press enter button
- then fill the form above and press the register button.
- if the process is successful it will show notification as below
- now we look at the database whether the data has been entered or not.
okay the data we input on the form has been stored in the database
- Okay, the tutorial is over, now it's your turn to try it.
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors