How to insert into database php and mysql

In this tutorial we will learn how to insert into database through php and mysql.php is server side scripting language designed for web development. While MYSQL is a open source Relational database management system that uses structured query language (SQL).
 To insert data into database we used INSERT INTO statement with appropriate values. after that we will execute the SQL query by passing into mysql_query() function to insert data into database.
First step is to create database and table in the mysql . open your favorite browser and type localhost in the url and press enter. Click on phpmy admin and follow the following steps to create database and tables.
Note: To execute php code and to create database you must install wamp or xamp.
Step 1: To creat Database Enter the database name and click on the create button a database will be created.

how to insert into database in php and mysql
Step 2: Create Table Enter the table name and number of fields which you want to create for this example I am create 4 fields.
insert into database
Step 3 : Enter the fields name.Follow the following image.
insertion into database php and mysql
Step 4: Now write the Following Html code to make a form, fields and a submit button. Method will be post and add a php file name in action. When the user clicked on the submit button the form data will be submitted to the page specified in the action.
 <form name="form1" method="post" action="form.php">  
  <table width="200" border="1" align="center">  
   <tr>  
    <th scope="row">Name</th>  
    <td><input type="text" name="name" id="name"></td>  
   </tr>  
   <tr>  
    <th scope="row">F.Name</th>  
    <td><input type="text" name="fname" id="fname"></td>  
   </tr>  
   <tr>  
    <th scope="row">Address</th>  
    <td><textarea name="address" id="address" cols="45" rows="5"></textarea></td>  
   </tr>  
   <tr>  
    <th colspan="2" scope="row"><input type="submit" name="submit" id="submit" value="Submit"></th>  
   </tr>  
  </table>  
 </form>  
Step 4: Now write the following php code to the file specified in action to insert data into database.First make a connection to the sever and select a database your created.Isset function is used that when a button is clicked execute the code in if statement. Now check your database table entry will be inserted successfully. It is simple example to insert into database you can extend this example.
 <?php  
                // connection to sever and database  
                $con=mysql_connect("localhost","root","");  
                mysql_select_db("database",$con);  
                //  
                if(isset($_POST['submit']))  
                {  
                     $name=$_POST['name'];  
                     $fname=$_POST['fname'];  
                     $address=$_POST['address'];  
                     // insert query to insert data into table.make sure first field values is empty becuase it is auto increment and primary key.  
                     mysql_query("insert into `information` values('','$name','$fname','$address')") or die(mysql_error());       
                }  
 ?>  
Share on Google Plus

About zeb

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment