All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Insert Data In Database Using PHP

Last Updated : Mar 11, 2024

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
<?php
    $connect=mysqli_connect("localhost","root","","index") or die("Connection Failed");
    if(!empty($_REQUEST['button']))
    {
        $user=$_REQUEST['username'];
        $email=$_REQUEST['email'];
        $pass=$_REQUEST['password'];
        $query=" INSERT INTO form(username, email, password) values($user, $email, $pass) ";
            if(mysqli_query($connect,$query))
            {
   echo “successfully data is stored in the database”;
            }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title> Registration Form </title>
</head>
<body>
    <div class="main">
        <div class="inner">
            <div class="welcome">
                Registration Form
            </div>
            <div class="registration_options">
                <form action="" method="post" enctype="multipart/form-data">
                    <div class="rleft_body">
                        <label for="">
                            Enter your name here
                        </label>
                        <br>
                        <label for="">
                            Enter your email here
                        </label>
                        <br>
                        <label for="">
                            Enter your password here
                        </label>
                    </div>
                    <div class="rright_body">
                        <input type="text" name="username" id="">
                        <br><br>
                        <input type="email" name="email" id="">
                        <br><br>
                        <input type="password" name="password" id="">
                    </div>
                    <div class="registration_submit">
                        <input type="submit" value="Save" id="save" name="button">
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
  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.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪