Saturday, May 7, 2011

How Insert data in Database in PHP

Hello Friends,
            In previous two blog we studied how to create database and how to connect with that database in PHP MySql. Now in this blog we are going to learn something very important for any site and that is How to enter or store the data in the database in PHP. In very simple word fill the registration form and click on the submit button  and data is stored in the database. So in this blog we are going to do the same thing. So let's start.


To perform this task we have to follow the following steps and you will get it very easily.
1. Fetch the data from the previous page using "name" property of input tag.
2. Write the query for insert the data in the database(simple insert query).
3. Execute(Run) that query.


That's it.


Let describe each step in detail. As I said earlier we will use the "StudentMaster" as 'our database and "StudentInfo" as our table of database.First of I tell you the fields (Columns ) of the table. ID(auto increment),FName,LName,PhoneNumber,EmailId,Password.


1. Fetch the data from the previous page using "name" property of input tag.
Let me show what is meaning of name property.
For example in previous page you have written "input ytpe="text name="FName"" then you have to fetch the value using the name propery i.e.FName.


You can fetch the value using GET,POST and REQUEST.If you are passing value using GET, you can fetch using GET and REQUEST. Same as you are passing value using POST,you can fetch using POST and REQUEST. So every time we will use REQUEST to fetch value irrespective of which method we used to pass the value. To fetch value following is the syntax.
$variablename=$_REQUEST/GET/POST['name property'];


So here to fetch the value of FName 
$fname=$_REQUEST['FName'];
fetch the value of ecach and every input as shown below:
$lname=$_REQUEST['LName'];
$phone=$_REQUEST['PNO'];
$email=$_REQUEST['EmailId'];
$pass=$_REQUEST['Pass'];


2. Write the query for insert the data in the database(simple insert query).
Now we have all the value to insert in the database. All the value we have stored in the respective variable name. We will use those variable. 
Simple insert query looks like :
insert into tblname(columns name) values ('values');
So in our example
$query="insert into StudentInfo (FName,LName,PhoneNumber,EmailId,Password) values ('$fname','$lname',$phone,'$email','$pass')";
Where $query is variable to store the query.


3.Execute(Run) the query.
We created the insert query in second step and we will run in this step.
To run the query we have MySql inbuilt function mysql_query(query)
So the final statement will be
mysql_query($query) or die ( mysql_error());
Where $query is our query created in step  and die I explained in previous blog.


So the final code is 

$fname=$_REQUEST['FName'];
$lname=$_REQUEST['LName'];
$phone=$_REQUEST['PNO'];
$email=$_REQUEST['EmailId'];
$pass=$_REQUEST['Pass'];

$query="insert into StudentInfo (FName,LName,PhoneNumber,EmailId,Password) values ('$fname','$lname',$phone,'$email','$pass')";

mysql_query($query) or die (mysql_error());

So please try this code and leave the comment if you like or If you have any doubts.
I'll wait for your response.

No comments:

Post a Comment