Monday, 18 January 2016

How to insert data into Database using Php and MySqli

Today I will teach you how to create PHP data insert script, using the Text, Html, CSS, Php, Mysqli.
For the data sending or retrieving it is important to use form element or we can say tags.
Like this <form method=”post” action=””></form>.
We are using method POST.
We have not set action attribute because we are giving action to current page.
  1. Create new folder in www name this folder Student_Record
  2. Create New File as index.php in your Student_Record folder
  3. Now Open your browser chrome or firefox type in address tab localhost/phpmyadmin

Make sure your wamp server or xamp server is turned on.

Create new database name this database record.

Then click on record and create new table in record.
Name the table student

Your student table structure will look like this.

Create columns like Name, Email, City, Website, Father, Mother. Give the VARCHAR to all, set the value 100 to all columns.
Your connection script will look like this
$connection = mysqli_connect("localhost","root","","record");

PHP MySQLi = PHP MySQL Improved!
Edit your index.php and write script for inserting data in your Html elements, write code below.
Here is the full version of index.php file.
<html lang="en-US">
    <meta charset="UTF-8">
    <title>Insert Record</title>
    <style type="text/css">
        margin:0; padding: 0;
        width:60%; background:#eee; margin:0 auto; margin-top:20px;
        background:#ffba34; font-family:verdana; color:#fff; margin:10px 0px;padding:10px;
        width:320px; height:40px; font-size:20px; font-family:century gothic;
        outline:none;  font-weight: normal;
    tr td{
        font-family:verdana; font-size:20px; background:#ed6c9e; padding:10px; color:#fff;
        width:427px; cursor:pointer;transition: background 0.5s;border:1px solid #fff; height:50px; background:#fff;font-size:20px;float:left; border:none;
        background:#ffba34; color:#fff; border:1px solid #fff;
    <div class="container">
    <form action="" method="post">
        <h1>Insert Record Using PHP Mysqli</h1>
        <tr><td>Name</td><td><input type="text" name="name" id="" placeholder="Enter Name.."/></td></tr>
        <tr><td>Email</td><td><input type="text" name="email" id="" placeholder="Enter Email.."/></td></tr>
        <tr><td>City</td><td><input type="text" name="city" id="" placeholder="Enter City.."/></td></tr>
        <tr><td>Website</td><td><input type="text" name="website" id="" placeholder="Enter Website.."/></td></tr>
        <tr><td>Father</td><td><input type="text" name="father" id="" placeholder="Enter Father.."/></td></tr>
        <tr><td>Mother</td><td><input type="text" name="mother" id="" placeholder="Enter Mother.."/></td></tr>
        <tr><td colspan="2"><input type="submit" name="submit" value="Insert Record" id="" /></td></tr>
$connection = mysqli_connect("localhost","root","","record");
$name = $_POST['name'];
$email = $_POST['email'];
$city = $_POST['city'];
$website = $_POST['website'];
$father = $_POST['father'];
$mother = $_POST['mother'];
$query = "insert into student(name,email,city,website,father,mother) values('$name', '$email', '$city', '$website', '$father', '$mother')";
    <center><h1 style="red">Author Abuzar Ansari</h1></center>

Let’s explain the php script
Hope you have learned the php basic, basic will help you.
We use if, for the conditional statement like in our script it is saying if isset, isset is a php function that check the value is set or not.
If submit button is clicked, that mean button is set, so our if statement will execute.
$name is a local variable which is holding the value of ‘name’ which is coming from the user input.
All the variables are holding the values that are coming to user input.
We have set variables names.
We created $query local variable which is holding the query values, or we can say what query we would like to do.
$query is saying -> insert data into student table, we entered column name like name, email, city, website, father mother, which are available in student table.
We will enter the value from users input, Like ‘$name’ will take the first place $email take second place as described in $query variable.
After this we will use mysqli_query(); function, which is used to run your query in this function we will set two parameters mysqli_query($connection,$query); first parameter for database connection second parameter for query.
Now Go To localhost/phpmyadmin and check the records database and student table, We have successfully submitted data. If any query comment below.