Creating a Simple Registration Using MySQLi Prepared Statement

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
  2. `user_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `username` varchar(50) NOT NULL,
  4. `password` varchar(50) NOT NULL,
  5. PRIMARY KEY (`user_id`)
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"
  1. <div id = "content">
  2. <form method = "POST" action = "submit_query.php">
  3. <table>
  4. <tr>
  5. <td><label>Username:</label></td>
  6. <td><input type = "text" name = "username" required = "required"></td>
  7. </tr>
  8. <tr>
  9. <td><label>Password:</label></td>
  10. <td><input type = "password" name = "password" required = "required"></td>
  11. </tr>
  12. <tr>
  13. <td colspan = "2"><br /></td>
  14. </tr>
  15. <tr>
  16. <td colspan = "2"><button style = "width:100%;" name = "save">Save</button></td>
  17. </tr>
  18. </table>
  19. </form>
  20. </div>
Then, we will add some design for the form, just copy and paste it inside the head tag
  1. <style>
  2. #content{
  3. background-color: #ffeecc;
  4. width:280px;
  5. height:110px;
  6. margin: 100px auto;
  7. padding:10px;
  8. border-radius:2px;
  9. -webkit-box-shadow:0px 0px 30px 3px #000;
  10. }
  11. </style>
Next, we will create the database connection, just copy the code below, then name it "connect.php"
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'sample');
  3. if($conn->connect_error){
  4. die("Fatal Error: Can't connect to database: ". $conn->connect_error);
  5. }
  6. ?>
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"
  1. <?php
  2. require_once 'connect.php';
  3. if(ISSET($_POST['save'])){
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  6. $func = $conn->prepare("INSERT INTO `user` (username, password) VALUES(?, ?)");
  7. $func->bind_param("ss", $username, $password);
  8. $func->execute();
  9. $func->close();
  10. $conn->close();
  11. header('location:index.php');
  12. }
  13. ?>
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!!!



Submitted by… (not verified)on Thu, 09/26/2019 - 19:16

This tutorial doesn't even work

Add new comment