How to connect HTML Register Form to MySQL Database with PHP

Learn, How to connect HTML Register Form to MySQL Database with PHP with simple coding and easy video.
First, you have to make a form using HTML code which I provide below.

HTML form (form.html)

  <title>Register Form</title>
 <form action="insert.php" method="POST">
    <td>Name :</td>
    <td><input type="text" name="username" required></td>
    <td>Password :</td>
    <td><input type="password" name="password" required></td>
    <td>Gender :</td>
     <input type="radio" name="gender" value="m" required>Male
     <input type="radio" name="gender" value="f" required>Female
    <td>Email :</td>
    <td><input type="email" name="email" required></td>
    <td>Phone no :</td>
     <select name="phoneCode" required>
      <option selected hidden value="">Select Code</option>
      <option value="977">977</option>
      <option value="978">978</option>
      <option value="979">979</option>
      <option value="973">973</option>
      <option value="972">972</option>
      <option value="974">974</option>
     <input type="phone" name="phone" required>
    <td><input type="submit" value="Submit"></td>

Inside the above code, you can see <form action="insert.php" method="POST">where, in action, I write insert.php which means we are sending our form data to insert.php.

The output of the above code

How to connect HTML Register Form to MySQL Database with PHP
Registration form in HTML

Server-side code (insert.php)

$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$phoneCode = $_POST['phoneCode'];
$phone = $_POST['phone'];
if (!empty($username) || !empty($password) || !empty($gender) || !empty($email) || !empty($phoneCode) || !empty($phone)) {
 $host = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbname = "youtube";
    //create connection
    $conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
    if (mysqli_connect_error()) {
     die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
    } else {
     $SELECT = "SELECT email From register Where email = ? Limit 1";
     $INSERT = "INSERT Into register (username, password, gender, email, phoneCode, phone) values(?, ?, ?, ?, ?, ?)";
     //Prepare statement
     $stmt = $conn->prepare($SELECT);
     $stmt->bind_param("s", $email);
     $rnum = $stmt->num_rows;
     if ($rnum==0) {
      $stmt = $conn->prepare($INSERT);
      $stmt->bind_param("ssssii", $username, $password, $gender, $email, $phoneCode, $phone);
      echo "New record inserted sucessfully";
     } else {
      echo "Someone already register using this email";
} else {
 echo "All field are required";

Explaining server-side code

Declaring variables and check, are they empty or not

By using a code from a line 2 to 7, we are grabbing data from HTML form and store it into PHP variables ( $username, $password, $gender, $email, $phoneCode and $phone).

Then, in line 8, we check these variables ( $username, $password, $gender, $email, $phoneCode and $phone) are not empty. If these variables are empty then we go all the way down to line 39. If these variables are not empty then code from line 9 will execute.

Make a database connection

From line 9 to line 12 we declare a variables ( $host, $dbUsername, $dbPassword and $dbname) and assign a value for connecting a database.

Then, in line 14 we make a database connection and assign it into a $conn

After that, in line 15 we check that, is our database connected. If there is no connection then we display error and exit. If a database is connected we continue from line 17.

Declare SQL query in a variable

In, line 18, there is a SELECT query to select data from a database and in line 19, there is an INSERT query to insert user record into a database.

Using the prepared statement

After that, we are using the prepared statement to run our query and protect our database from SQL injection.

21. We Run SELECT query

22. We are Passing an email value into a SELECT query and we define “s” because email is a string. You can see below and know, which parameter you should use your data type.

  • i – integer
  • d – double
  • s – string
  • b – BLOB

You can learn more about above i, d, s, and b in Prepared Statements

In a SELECT query, we are selecting only email so, in line 24, we catch that email value.

In, line 26, we store the number of row in a variable  $rnum from our result.

Insert user’s data

Then in, line 27, we check is our result contains 0 rows, if yes then we continue running our coding if not, we jump into line 33 and give a message “Someone already registers using this email”.

If there are zero rows in a result then that means, this user’s data is not already stored in our database. So we can store this record in our database as a new user. But first, we need to close a prepared statement for the SELECT query and start a prepared statement for an INSERT query.

And after data is inserted into a database, we give a message “New record inserted successfully”. And, that’s it.

This is how you connect how to create registration form in HTML with database.

You may also like to read: