Sunday, June 14, 2009

How to edit data of Mysql using php

You have already familiar with the how to access mysql database as well as how to insert data into mysql database. How to access mysql database topic is discussed on http://arjudba.blogspot.com/2009/06/how-to-access-mysql-database-using-php.html and how to insert data into mysql database is discussed on http://arjudba.blogspot.com/2009/06/how-to-insert-data-into-mysql-database.html.

Using the insert php code which is discussed in the above link we have inserted 3 rows into basic_info table of the mysql database. Querying basic_info from mysql database yields following result,

mysql> select * from basic_info;
+---------------------------+------+----------------+
| name | age | phone |
+---------------------------+------+----------------+
| Mohammad Abdul Kasem | 65 | 01734060483 |
| Fariha Tasnuva Queen | 23 | +01912046954 |
| Mohammad Abdul Momin Arju | 23 | +8801710282272 |
+---------------------------+------+----------------+
3 rows in set (0.00 sec)

Now using php code we like to update the row based on phone number. At first in the browser it will ask phone number and then corresponding row will be displayed where user will edit information and then submit. In the mysql database that row will be updated.

Step by step whole process is discussed.

1)Create form which will take phone number from user.
The following code will create a form which will create a form that will take phone number from user and also it contains a 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" >Enter Phone no. to edit: </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; padding-left:455px;" border="0" cellpadding="0" cellspacing="1" width="50%">
<tbody>
<tr>
<td colspan="2" align="right" bgcolor="#dadce1" height="4">
<input name="submit" value="submit" type="submit">
</td>
</tr>
</tbody>
</table>

</body>
</html>

2)Display data from the database based on corresponding phone number.
We need php code to display data based on the phone number he typed on to browser. Below is the simple piece of code and you will understand easily the code if you have gone through my previous two posts.
It is displayed a submit button which in fact will update data into database.

<?php
#Form Variables.
$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');

#A query is written which will extract all information
#from basic_info table based on phone number entered.

$query = "SELECT * FROM basic_info where phone='".$phone."'";
#Execute query and store result into variable $result.
#echo $query;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$name=$row["name"];
$age=$row["age"];
$phone=$row["phone"];
}

echo "<form method=\"post\">";
echo "<table style=\"margin-bottom: 1px;\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"60%\">\n";
echo "\t<tr>\n";
echo "\t\t<td bgcolor=\"#dadce1\" colspan=\"2\">Information of Phone:\t $phone</td>";
echo "\t\t<td bgcolor=\"#dadce1\"> <input type=\"hidden\" value='$phone' name=\"phone\" size=\"50\" ></td>";

echo "\t</tr>\n";

echo "\t<tr>\n";
echo "\t\t<td bgcolor=\"#dadce1\">Name:</td>";
echo "\t\t<td bgcolor=\"#dadce1\"> <input type=\"text\" value='$name' name=\"name\" size=\"50\" ></td>";
echo "\t</tr>\n";

echo "\t<tr>\n";
echo "\t\t<td bgcolor=\"#dadce1\">Age:</td>";
echo "\t\t<td bgcolor=\"#dadce1\"> <input type=\"text\" value='$age' name=\"age\" size=\"2\"></td>";
echo "\t</tr>\n";

echo "</table>\n";

echo "<table style=\"margin-bottom: 1px;\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"40%\">";
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\" bgcolor=\"#dadce1\" height=\"4\">";
echo "<input name=\"submit02\" value=\"submit\" type=\"submit\">";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
#Free up Memory for $result.

mysql_free_result($result);
#Close database connection.
mysql_close($link);
}
?>

3)Update data into database upon pressing submit button:
Here also we need to write php code to update data into database. With a hidden field
phone number is passed to another form and using phone number data is updated in the database.
Below is the php code that will handle the update.

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

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

if(isset($_POST['submit02'])){
$phone=$_POST["phone"];
$age=$_POST["age"];
$name=$_POST["name"];

#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');

#A query is written which will extract all information
#from basic_info table based on phone number entered.

$query = "update basic_info
set name='".$name."',
age='".$age."'
where
phone='".$phone."'";
#echo $query;
#Execute query and store result into variable $result.
#echo $query;
mysql_query($query) or die('Query failed: ' . mysql_error());
echo "Update Successfully done";
#Close database connection.
mysql_close($link);
}
?>

4)Run the code.
Total code is look like,

<?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");

#Close database connection.
mysql_close($link);

}
?>

<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>

Now save it into a file and load into browser.

It will be displayed as after submitting phone number +8801710282272,
Enter Phone no. to edit:
Information of Phone: +8801710282272
Name:
Age:


Edit name and/or age field in the browser and press submit button.

Now I changed it to,
Enter Phone no. to edit:
Information of Phone: +8801710282272
Name:
Age:


After pressing submit button it shows,
Update Successfully done

and now from database I issue query and it shows.

mysql> select * from basic_info;
+----------------------+------+----------------+
| name | age | phone |
+----------------------+------+----------------+
| Mohammad Abdul Kasem | 65 | 01734060483 |
| Fariha Tasnuva Queen | 23 | +01912046954 |
| Arju | 24 | +8801710282272 |
+----------------------+------+----------------+
3 rows in set (0.00 sec)

And row is updated.

In fact you can do same query and display output in your browser with the help of post,
http://arjudba.blogspot.com/2009/06/how-to-access-mysql-database-using-php.html
Related Documents
http://arjudba.blogspot.com/2009/06/how-to-access-mysql-database-using-php.html
http://arjudba.blogspot.com/2009/06/how-to-insert-data-into-mysql-database.html

1 comment: