All TalkersCode Topics

Follow TalkersCode On Social Media

How To Insert Data In Database Using PHP

Last Updated : Jul 1, 2023

How To Insert Data In Database Using PHP

In this tutorial we will show you the solution of how to insert data in database using PHP, there are many ways with help of which we can insert data into the database. In this tutorial, we are going to use the MySQL method.

Now, let us understand how to use this method in our codes with the help of the example given below.

Step By Step Guide On How To Insert Data In Database Using PHP :-

Here, first of all, we have to create a database and a PHP form also to insert data into the database.

The fields which we are going to specify in form for database purposes must be available in the database with the same name because of case sensitiveness.

Now, let us see in brief this concept using forms.

// registration.php
    $connect=mysqli_connect("localhost","root","","index") or die("Connection Failed");
        $query=" INSERT INTO form(username, email, password) values($user, $email, $pass) ";
   echo “successfully data is stored in the database”;
<!DOCTYPE html>
<html lang="en">
    <title> Registration Form </title>
    <div class="main">
        <div class="inner">
            <div class="welcome">
                Registration Form
            <div class="registration_options">
                <form action="" method="post" enctype="multipart/form-data">
                    <div class="rleft_body">
                        <label for="">
                            Enter your name here
                        <label for="">
                            Enter your email here
                        <label for="">
                            Enter your password here
                    <div class="rright_body">
                        <input type="text" name="username" id="">
                        <input type="email" name="email" id="">
                        <input type="password" name="password" id="">
                    <div class="registration_submit">
                        <input type="submit" value="Save" id="save" name="button">
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on the browser’s screen or display is always written inside these codes.
  5. Here, as you see to show you how to insert data into the database, we use the example of the registration form. As you can see that we create some input fields here which send data to the database with a button click. The database used here is an index and the table name is form. The database with the table must be present in your database. Whereas required columns are user, email, and pass.
  6. We use some validations here like the form will not be submitted if any field is empty. Then we apply validations on email, passwords, etc. if all conditions are verified then data will be submitted to the database.
  7. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to insert data into a database using PHP.

I hope this tutorial on how to insert data in database using PHP helps you and the steps and method mentioned above are easy to follow and implement.