Register Form PHP dan MYSQLi
Baiklah kali ini kita akan membuat Form Register dengan PHP & MYSQLi , Untuk membuat aplikasi ini anda dapat menggunakan text editor kesayangan anda dan baca tutorial dibawah ini, kemudian download source code yang di berikan
Baca Jua Ya :
Dalam projek kali ini kita akan membuat 2 buah file yaitu :
- index.php
- db.php
Langkah yang pertama buatlah database dengan nama db_register, kemudian buat table "users" , anda dapat mengcopy scirpt dibawah ini :
CREATE TABLE `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `username` (`email`)
)
Setelah itu buat file dengan nama db.php , untuk koneksi yang Menghubungkan aplikasi ke database :
<?php
$host = 'localhost';
$user = 'root';
$psw = '';
$db_name = 'db_register';
// Koneksi di database
$koneksi = mysqli_connect($host, $user, $psw, $db_name);
if ( mysqli_connect_errno() ) {
// Jika koneksi Error tampilkan Pesan.
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
?>
Kemudian kita buat file index.php , dan berikut ini adalah script untuk membuat form Register :
<div id="wrapper">
<H2><a href="https://senterwebs.com/artikel-44/register-form-php-dan-mysqli.html">
Form Register dengan PHP dan MYSQLi</a></H2>
<h3><a href="https://senterwebs.com/artikel-44/register-form-php-dan-mysqli.html">Back to tutorial senterwebs.com</a></h3>
<form method="post" action="">
<fieldset>
<legend>Registration Form</legend>
<table width="400" border="0" cellpadding="10" cellspacing="10">
<tr>
<td colspan="2" align="center" class="error"><?php echo $msg;?></td>
</tr>
<tr>
<td style="font-weight: bold"><div align="right"><label for="name">Name</label></div></td>
<td><input name="name" type="text" class="input" size="25" required /></td>
</tr>
<tr>
<td style="font-weight: bold"><div align="right"><label for="email">Email</label></div></td>
<td><input name="email" type="email" class="input" size="25" required /></td>
</tr>
<tr>
<td height="23" style="font-weight: bold"><div align="right"><label for="password">Password</label></div></td>
<td><input name="password" type="password" class="input" size="25" required /></td>
</tr>
<tr>
<td height="23"></td>
<td><div align="right">
<input type="submit" name="submit" value="Register!" />
</div></td>
</tr>
</table>
</fieldset>
</form>
</div>
Untuk Script prosesnya kita lakukan di file itu sendiri, berikut ini potongan dari script nya , tempatkan di file index.php:
<?php
include ("db.php");
$msg = "";
if(isset($_POST["submit"]))
{
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$name = mysqli_real_escape_string($koneksi, $name);
$email = mysqli_real_escape_string($koneksi, $email);
$password = mysqli_real_escape_string($koneksi, $password);
$password = md5($password);
$sql="SELECT email FROM users WHERE email='$email'";
$result=mysqli_query($koneksi,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result) == 1)
{
$msg = "Sorry...This email already exist...";
}
else
{
$query = mysqli_query($koneksi, "INSERT INTO users (name, email, password)VALUES ('$name', '$email', '$password')");
if($query)
{
$msg = "Thank You! you are now registered.";
}
}
}
?>
All script index.php
<?php
include ("db.php");
$msg = "";
if(isset($_POST["submit"]))
{
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$name = mysqli_real_escape_string($koneksi, $name);
$email = mysqli_real_escape_string($koneksi, $email);
$password = mysqli_real_escape_string($koneksi, $password);
$password = md5($password);
$sql="SELECT email FROM users WHERE email='$email'";
$result=mysqli_query($koneksi,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result) == 1)
{
$msg = "Sorry...This email already exist...";
}
else
{
$query = mysqli_query($koneksi, "INSERT INTO users (name, email, password)VALUES ('$name', '$email', '$password')");
if($query)
{
$msg = "Thank You! you are now registered.";
}
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="Senterwebs">
<title>Registration Form PHP MYSQLi by Senterwebs dot Com</title>
<link rel="shortcut icon" href="https://senterwebs.com/favicon.png">
<link rel="icon" href="https://senterwebs.com/favicon.png">
<!-- style CSS utama -->
<style>
@import url(http://fonts.googleapis.com/css?family=Bree+Serif);
#wrapper{width:500px;margin:0 auto;}
.ac {
height: 40px;
width: 100px;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
position: fixed;
z-index: 9999;
}
.cc {
position: relative;
}
body {
background-color: #19B27D;
}
a {
color:#FFF;
text-decoration:none;
}
h3,h2,h1 {font-family: 'Bree Serif', 'serif';color:#FFF;text-align: center;}
h1 {
margin-top: 60px;
font-size:50px;
}
h2{ margin-top:30px;
font-size:40px;}
label
{
font-family:Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace;
font-size:20px;
font-weight:bold;
}
.input
{
padding:5px;
font-family:Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace;
font-size:18px;
}
input[type=submit]
{
padding:5px;
font-family:Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace;
font-size:18px;
font-weight:bold;
background:#F94A4A;
border:2px solid black;
color:#FFF;
}
fieldset
{
width: 500px;
}
fieldset legend
{
padding:2px;
font-family:Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace;
font-size:14px;
font-weight:bold;
background:#F94A4A;
border:2px solid black;
color:#FFF;
}
.error
{
color:red;
font-family:Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace;
font-size:16px;
}
</style>
</head>
<body>
<div id="wrapper">
<H2><a href="https://senterwebs.com/artikel-44/register-form-php-dan-mysqli.html">
Form Register dengan PHP dan MYSQLi</a></H2>
<h3><a href="https://senterwebs.com/artikel-44/register-form-php-dan-mysqli.html">Back to tutorial senterwebs.com</a></h3>
<form method="post" action="">
<fieldset>
<legend>Registration Form</legend>
<table width="400" border="0" cellpadding="10" cellspacing="10">
<tr>
<td colspan="2" align="center" class="error"><?php echo $msg;?></td>
</tr>
<tr>
<td style="font-weight: bold"><div align="right"><label for="name">Name</label></div></td>
<td><input name="name" type="text" class="input" size="25" required /></td>
</tr>
<tr>
<td style="font-weight: bold"><div align="right"><label for="email">Email</label></div></td>
<td><input name="email" type="email" class="input" size="25" required /></td>
</tr>
<tr>
<td height="23" style="font-weight: bold"><div align="right"><label for="password">Password</label></div></td>
<td><input name="password" type="password" class="input" size="25" required /></td>
</tr>
<tr>
<td height="23"></td>
<td><div align="right">
<input type="submit" name="submit" value="Register!" />
</div></td>
</tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>
Untuk Source Code Lengkap Membuat Form Register Dengan PHP dan MYSQLi anda bisa Download dan Melihat Demo Programnya Disini