How to Insert Data in Database using PHP and mySql

How-to-Insert-Data-in-Database-using-PHP-and-mySql

Today I will explain how to insert data or records in database using PHP  and mySql. This topic is very essential part of any web developer at beginner to advance level.

I know most of people recommended mysql database because it is free and easy to understand. This post is simple insert data or records in database later I will explain how to get or fetch data from database and how to update data or records in database also how to delete data or records from database using php and mysql.

Also I will explain in this post how to create database using phyMyadmin in simple way. So lets start first general topic insert data or records in database.

Follow These Steps.

Step 1.   First Create Database

Go to browser and type localhost/phpmyadmin screen shown below


Insert Data using PHP and mySql

Now, fill up database name and collation, put any name you want for example, I put tutorials as a database name and utf8_general_ci as a collation name screen shown below


Insert Data using PHP and mySql

then click create button, after that you can easily seen in left side at phpMyadmin window, tutorials database is ready for use, also you can seen message is no tables found in database, screen shown below


Insert Data using PHP and mySql

Step 2.   Create Table

Now, fill up any name of table you want for example, I put students as a table name  and number of columns 6. I will design table with four fields stu_id, stu_name, stu_gender, stu_email, stu_phone, stu_address also make stu_id field  as a primary key auto incremented then click Go button, screen shown below


Insert Data using PHP and mySql

Now, fill up required data screen shown below.


Insert Data using PHP and mySql

click save button after that easily you can see table is ready for use, screen shown below


Insert Data using PHP and mySql

Step 3.   Create a Project Folder

Go to where xampp is installed and open htdoc folder and create a new folder for example, folder name like tutorials and create a file with extension .php like addstu.php , Code shown below

      <!DOCTYPE html>
      <html lang="en">
      <head>
        <title>Add Students Records</title> 
      </head>
      <body>
      <form action="insertstu.php" method="post">
        <table align="center" border="1">
        <tbody>
          <tr>
            <td>Name</td>
            <td><input name="name" placeholder="Name" type="text" required </td>
           <tr>
             <td>Gender</td>
             <td>
                <input name="gender" type="radio" value="male" checked="">Male
                <input name="gender" type="radio" value="female">Female
             </td>
          </tr>
          <tr>
             <td>Email Id.</td>
             <td><input name="email" id="email" placeholder="Email Id."  type="text" required></td>
          </tr>
          <tr>
            <td>Phone No.</td>
            <td><input name="phone" id="phone" placeholder="Phone No." type="text" maxlength="10" required></td>
         </tr>
         <tr>
           <td>Address:</td>
           <td><textarea name="address" placeholder="Address" rows="5"></textarea></td>
         </tr>
         <tr>
            <td></td>
            <td><input type="submit" name="submit" value="Submit"></td>
         </tr>
       </tbody>
     </table>
   </form>
 </body>
</html>

Step 4.   Go to browser and type localhost/tutorials/addstu.php screen shown below


Insert Data using PHP and mySql

Step 5.   Create Config File

I will create config.php in directory folder for database connection, code shown below

           <?php

             $servername = "localhost";
             $username   = "root";
             $password   = "";
             $dbname     = "tutorials";
            
             // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            
            // Check connection
            if ($conn->connect_error) 
              { die("Connection failed: " . $conn->connect_error);
              }

          ?>

Step 6.   Create Insert File

I will create insertstu.php in directory folder, code shown below

          <?php

              include_once('config.php');

              $name    = $_POST['name'];
              $gender  = $_POST['gender'];
              $email   = $_POST['email'];
              $phone   = $_POST['phone'];
              $address = $_POST['address'];

              $data = "insert into students (stu_name, stu_gender, stu_email, stu_phone, stu_address) values ('$name', '$gender', '$email', '$phone', '$address')";
              $result = mysqli_query($conn, $data);

              if(isset($result))
                {
                   echo "<script>alert('Do you want to  insert new student record');window.location='addstu.php?p=1';</script>";
                }
             else
                {
                echo "Student Entry not Inserted";
                }

          ?>

Step 7.   Now, go to browser type localhost/tutorials/addstu.php and insert data in required filed then press submit button, then go to database and click students table and check data is inserted or not, screen shown below.


Insert Data using PHP and mySql





                   I hope, it helped you to understand, how to insert data in database using PHP and mySql......Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

9 comments:

Anonymous said...

This code is wrong and is vulnerable to SQL injection. Use prepared statements.

The author of this article needs to stop teaching.

saranya said...

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.php training in chennai

saranya said...

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here. https://www.besanttechnologies.com/training-courses/php-training/php-training-institute-in-chennai

Blogger said...

Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Blog.Please feel free to visit our site. Thank you for sharing.
php training center in indore
Keep Posting:)

Blogger said...

Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Blog.Please feel free to visit our site. Thank you for sharing.
php training center in indore
Keep Posting:)

Blogger said...

Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Blog.Please feel free to visit our site. Thank you for sharing.
php training center in indore
Keep Posting:)

Anonymous said...

thank u blogger

saranya said...

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here. php training in chennai

Unknown said...

Wonderful post.Thanks for sharing this information to my vision which was very informative.
Php Developers Chennai | Php Developers India


Post a Comment

Thanks For Visiting Blog...