How to insert data to MySQL using PHP

Hi friends.. Today i am going to tell you about how to insert data available in HTML from using PHP in to MySQL database. Here i will use post method for transfer data using HTML form to my PHP script. Lets see here how to create form without having table. I mean you can create a HTML form and you want to align it in proper manner. In here you can see inside my css code i have aligned it without using table .OK  here is my code for create HTML form

Below you can seemy css code for displaying the proper out put like this

To change this template, choose Tools | Templates
and open the template in the editor.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <style type="text/css">

            body {background-color:black;}

            #main-body  {width: 350px;
                        height: 250px;
                        background-color: cornflowerblue;
                        border: 6px brown dashed;
                        margin-left: 300px;
                        margin-top: 200px;
                        padding: 10px;


                        width: 340px;
                        height: 240px;

                        background-color: white;
                        margin-left: 5px;


          #myform   {padding: 30px;}

          #myform label,input{ display: block;
                                width: 120px;
                                float: left;
                                margin-bottom: 10px;

          #myform label{

                       text-align: right;
                        padding-right: 20px;

                        clear: left;

           #submit  {width: 75px; float: left; margin-left: 175px;}

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <div id="main-body">

          <div id="main-body-inner">

              <form id="myform" name="myform" action="insert.php" method="post">
                  <label for="first-name">First Name</label>
                  <input type="text" id="fname" name="fname" /> <br />
                  <label for="last-name">Last Name</label>
                  <input type="text" id="lname" name="lname" /><br />
                  <label for="age">Age</label>
                  <input type="text" id="age" name="age" /><br />

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


Now we have created our html form and we have styled it. Below you can see my PHP code.

 * author
include 'connection.php';

    if(isset ($_POST['fname']) && isset ($_POST[ 'lname']) && isset ($_POST[ 'age'])) // getting the values from the post

    $connection = new createConnection(); // creating connection

    $connect = $connection->connectToDatabase(); // connect to database

    $connection->selectDatabase(); // select database

    $fname = mysql_real_escape_string($_POST['fname']); // escape sql injection
    $lname =mysql_real_escape_string($_POST['lname']);
    $age = mysql_real_escape_string($_POST['age']);

    $query="insert into test  values ('$fname','$lname','$age')"; // query for insert data

    mysql_query($query); // execute the insert query

   echo ("User registration Successfull.."); 

   echo "<a href='index.html'><h1>Go Back </h1> </a>"; // link for go back

   $connection->closeConnection(); // closing the connection

                echo "Error occured!"; // display error message

In here i have used a connection class. Below you can see my connection class.


 * @author damith
 * @copyright 2011

class createConnection
    var $host="localhost";
    var $username="username here";
    Var $password=" password here";
    var $database="databse name";
    var $myconn;

    function connectToDatabase()

        $conn= mysql_connect($this->host,$this->username,$this->password);

            die ("Cannot connect to the database");


            $this->myconn = $conn;


        return $this->myconn;


    function selectDatabase()


            echo "Cannot find the database ".$this->database;



    function closeConnection()




When you completed the insert process you can see the message like this.

OK you done.. Download the full source from here

Download this video
Download Source Code