Sunday, June 14, 2009

How to insert data into mysql database using php

How to access or read data from mysql database table using php is discussed in the topic http://arjudba.blogspot.com/2009/06/how-to-access-mysql-database-using-php.html. In this post I will discuss how to insert or write data into mysql database using php.

To do that, I will use a form and user will fill out the form and upon pressing submit button the data will be inserted into mysql database. So before inserting data into mysql database we need to setup database- that means we have the database table with appropriate fields.

1)Setup Database.

Let's create a database table named basic_info for mysql test database.
D:\xampp\mysql\bin>mysql -u root -ptest
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.30-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use test;
Database changed

mysql> create table basic_info(
-> name varchar(50),
-> age int(2),
-> phone varchar(20));
Query OK, 0 rows affected (0.22 sec)

mysql> select * from basic_info;
Empty set (0.06 sec)

2)Create a simple html form.
Below is my form which will display Name, age, phone field in the browser and a simple submit button.

<html>
<body>
<form method="post">
<table style="margin-bottom: 1px; padding-left:10px;" border="0" cellpadding="0"
cellspacing="1" width="75%">
<tbody>
<tr>
<td align="right" bgcolor="#e9ece9" >Name: </td>
<td bgcolor="#e9ece9" style="padding-left:4px;">
<input name="name" size="50" maxlength="50" width="50" value="<? echo $name; ?>">
</td>
</tr>

<tr>
<td align="right" bgcolor="#e9ece9" >Age: </td>
<td bgcolor="#e9ece9" style="padding-left:4px;">
<input name="age" size="2" maxlength="2" width="2" value="<? echo $age; ?>">
</td>
</tr>

<tr>
<td align="right" bgcolor="#e9ece9" >Phone: </td>
<td bgcolor="#e9ece9" style="padding-left:4px;" >
<input name="phone" size="20" maxlength="20" value="<? echo $phone; ?>">
</td>
</tr>
</tbody>
</table>

<table style="margin-bottom: 1px;" border="0" cellpadding="0" cellspacing="1" width="40%">
<tbody>
<tr>
<td colspan="2" align="right" bgcolor="#dadce1" height="4">
<input name="submit" value="submit" type="submit">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

3)Prepare php code that will insert data into database submitted into browser.

<?php
#Form Variables.
$name='';
$age='';
$phone='';

#Database connection strings.
$ip='localhost';
$mysqluser='root';
$mysqlpass='test';
$mysqldb='test';

if(isset($_POST['submit'])){
extract($_POST);

#Build connection to mysql server.
$link= mysql_connect($ip, $mysqlUser, $mysqlPass)
or die('Could not connect: ' . mysql_error());

#Select a database to which we will perform operation.
mysql_select_db($mysqldb) or die('Could not select database');

#Build query that will insert values into database
$query="insert into basic_info(name, age, phone) values('".$name."','".$age."','".$phone."')";

#Execute query
mysql_query($query) or die ("Error Inserting values into database");
}
?>

4)Combine php code with html form.

<?php
#Form Variables.
$name='';
$age='';
$phone='';

#Database connection strings.
$ip='localhost';
$mysqluser='root';
$mysqlpass='test';
$mysqldb='test';

if(isset($_POST['submit'])){
extract($_POST);

#Build connection to mysql server.
$link= mysql_connect($ip, $mysqlUser, $mysqlPass)
or die('Could not connect: ' . mysql_error());

#Select a database to which we will perform operation.
mysql_select_db($mysqldb) or die('Could not select database');

#Build query that will insert values into database
$query="insert into basic_info(name, age, phone) values('".$name."','".$age."','".$phone."')";

#Execute query
mysql_query($query) or die ("Error Inserting values into database");
}
?>

<html>
<body>
<form method="post">
<table style="margin-bottom: 1px; padding-left:10px;" border="0" cellpadding="0"
cellspacing="1" width="75%">
<tbody>
<tr>
<td align="right" bgcolor="#e9ece9" >Name: </td>
<td bgcolor="#e9ece9" style="padding-left:4px;">
<input name="name" size="50" maxlength="50" width="50" value="<? echo $name; ?>">
</td>
</tr>

<tr>
<td align="right" bgcolor="#e9ece9" >Age: </td>
<td bgcolor="#e9ece9" style="padding-left:4px;">
<input name="age" size="2" maxlength="2" width="2" value="<? echo $age; ?>">
</td>
</tr>

<tr>
<td align="right" bgcolor="#e9ece9" >Phone: </td>
<td bgcolor="#e9ece9" style="padding-left:4px;" >
<input name="phone" size="20" maxlength="20" value="<? echo $phone; ?>">
</td>
</tr>
</tbody>
</table>

<table style="margin-bottom: 1px;" border="0" cellpadding="0" cellspacing="1" width="40%">
<tbody>
<tr>
<td colspan="2" align="right" bgcolor="#dadce1" height="4">
<input name="submit" value="submit" type="submit">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

5)Run above code and fill fields and press submit button.
After running above code it will display following in your browser. Then I filled it like below.

Name:
Age:
Phone:


6)Query from database and be sure data is inserted.

mysql> select * from basic_info;
+---------------------------+------+----------------+
| name | age | phone |
+---------------------------+------+----------------+
| Mohammad Abdul Momin Arju | 23 | +8801710282272 |
+---------------------------+------+----------------+
1 row in set (0.03 sec)


Related Documents

No comments:

Post a Comment