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.
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.
Step 3 : Enter the fields name.Follow the following image.
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());
}
?>
Blogger Comment
Facebook Comment