In this tutorial, we will create a simple registration form using MySQLi prepared statement. it is an advanced and secured kind of script to prevent MySQL injection threat. Due to depreciated of MySQL most of the website are required to use MySQL / PDO statement. Now be prepared, let's start coding
1. We open the database server, then type localhost/phpmyadmin
2. Then name the database "sample"
3. After creating the database, click SQL and paste the code below
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Creating HTML form
After setting up the database, we will create the HTML form, The code below will display a simple registration form. To create a form, open any kind of text editor (notepad++, etc.). Then copy the code below, paste it inside the body tag, name it "index.php"
<div id = "content">
<form method = "POST" action = "submit_query.php">
<table>
<tr>
<td><label>Username:</label></td>
<td><input type = "text" name = "username" required = "required"></td>
</tr>
<tr>
<td><label>Password:</label></td>
<td><input type = "password" name = "password" required = "required"></td>
</tr>
<tr>
<td colspan = "2"><br /></td>
</tr>
<tr>
<td colspan = "2"><button style = "width:100%;" name = "save">Save</button></td>
</tr>
</table>
</form>
</div>
Then, we will add some design for the form, just copy and paste it inside the head tag
<style>
#content{
background-color: #ffeecc;
width:280px;
height:110px;
margin: 100px auto;
padding:10px;
border-radius:2px;
-webkit-box-shadow:0px 0px 30px 3px #000;
}
</style>
Next, we will create the database connection, just copy the code below, then name it "connect.php"
<?php
$conn = new mysqli('localhost', 'root', '', 'sample');
if($conn->connect_error){
die("Fatal Error: Can't connect to database: ". $conn->connect_error);
}
?>
Lastly, we will now create the script for saving the data from the HTML form. The code below holds the data that has been input in the HTML form, then it will directly send to database server. Just copy the code below, then name it "submit_query.php"
<?php
require_once 'connect.php';
if(ISSET($_POST['save'])){
$username = $_POST['username'];
$password = $_POST['password'];
$func = $conn->prepare("INSERT INTO `user` (username, password) VALUES(?, ?)");
$func->bind_param("ss", $username, $password);
$func->execute();
$func->close();
$conn->close();
-
}
?>
There you have it, we created a simple registration form. I hope that this tutorial help you to your projects. For more tutorials and updates, just kindly visit this site. Enjoy Coding!!!