Insert data into database using PHP

Today we are going to learn how we insert data into MySQL database using PHP. It is very simple if you follow this step by step tutorial of how we insert data in MySQL in PHP, before we start we have to install some software's which is listed below.
1. MySQL
2. Notepad++ or we can also use any Text Editor.
3. XAMPP Server.

After installing all the above software's we have to do some configuration in XAMPP server, for this open you web browser and type http://localhost/xampp/ and press enter it, you will see as show below.


Now click on PhpMyAdmin you get the login screen if you get any error message then for this i am giving you the complete step to install XAMPP.

Step1: Double click on your XAMPP software.



Step2: Next screen shown below you have to click Next button.






Step3. Next screen shown below here you must check all the check boxes otherwise you face problem in future.




Step4: Once software is install completely, open it you will see the below screen, here you see Apache, MySQL, FileZilla & Tomcat all are tick green and started already, if any of these shown cross then click on cross button it will install.



Step5: Now open your web browser and type http://localhost/xampp/ and press enter then click on PhpMyAdmin link on the left side of your page after that you get the login page.


Step6: If you try to login and click on Go button you get the below error message it means you have to set the password for the username root.



Goto xampp\phpmyadmin and open file config.inc.php in any text editor and write password as you want, i keep the password root as show below see the highlighted line.


Now again open http://localhost/xampp/ in your web browser and login with your password.

So till now we install all the required software's now we have to create our database.
1. create database employee_db;
2. use employee_db;
3. create table employee (first_name char(25), last_name char(25), email_address char(50));

Step1: index.php It is a form where we enter our data.

  <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>Add Record Form</title>

    </head>

    <body>

    <form action="insert.php" method="post">

        <p>

            <label for="firstName">First Name:</label>

            <input type="text" name="firstname" id="firstName">

        </p>

        <p>

            <label for="lastName">Last Name:</label>

            <input type="text" name="lastname" id="lastName">

        </p>

        <p>

            <label for="emailAddress">Email Address:</label>

            <input type="text" name="email" id="emailAddress">

        </p>

        <input type="submit" value="Submit">

    </form>
</body>
</html>
Step2. insert.php 

    <?php

    $link = mysqli_connect("localhost", "root", "root", "employee_db");


    if($link === false){

        die("Error " . mysqli_connect_error());

    }

    $first_name = mysqli_real_escape_string($link, $_POST['firstname']);

    $last_name = mysqli_real_escape_string($link, $_POST['lastname']);

    $email_address = mysqli_real_escape_string($link, $_POST['email']);

    $sql = "insert into employee (first_name, last_name, email_address) values ('$first_name', '$last_name', '$email_address')";

    if(mysqli_query($link, $sql)){

        echo "Records added successfully.";

    } else{

        echo "Error: Not able to execute $sql. " . mysqli_error($link);

    }

       mysqli_close($link);

    ?>


Note: If you still have any problem then write in comments I will help you.

Post a Comment

11 Comments

  1. Super Job, It solve my problem

    ReplyDelete
  2. Can you give me modify, delete and search code

    ReplyDelete
    Replies
    1. Everything is in my blog available so kindly read my previous posts.

      Delete
  3. Sir, I install XAMPP in the same way as you tell but my MySQL is not started in XAMPP control panel, when i click on start button it gives error, so pls. tell me what is the problem.

    ReplyDelete
    Replies
    1. Exit the XAMPP and restart your system if everything is alright then it will start otherwise you may do some mistake in installing XAMPP so reinstall it

      Delete
    2. Thanks sir my problem solved after restarting system

      Delete
  4. give me a sample project urgent needed

    ReplyDelete
    Replies
    1. ok rizwan i will send you, read my this post along with my previous post carefully it will solve your problem.

      Delete
  5. i am looking for how to make a search enigne in php. for example: when you type "p" you will find all words start with p. help please!

    ReplyDelete