Data storing in to database enables us to keep the information safe and secure, but what if we wish to update it. Here we are with the proper solution i.e. Update data in database using PHP. In this blog post we will show you  how to update previously stored information in database using PHP.

For this you must have a database in MySQL with the information stored in it. Here, we have a database named “company” which consists of table named “employee” with 5 fields viz.

“employee_id”, “employee_name”, “employee_email”, “employee_contact” and “employee_address”.

next we create a php page named “updatephp.php” where following steps will be performed:

  • Establish connection with server .
  • Selecting database.
  • Executing query.
  • Closing connection with server.

 Below is our complete code with download and live demo option  

 


PHP File: updatephp.php


<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="maindiv">
<div class="divA">
<div class="title">
<h2>Update Data Using PHP</h2>
</div>
<div class="divB">
<div class="divD">
<p>Click On Menu</p>
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("company", $connection);
if (isset($_GET['submit'])) {
$id = $_GET['did'];
$name = $_GET['dname'];
$email = $_GET['demail'];
$mobile = $_GET['dmobile'];
$address = $_GET['daddress'];
$query = mysql_query("update employee set
employee_name='$name', employee_email='$email', employee_contact='$mobile',
employee_address='$address' where employee_id='$id'", $connection);
}
$query = mysql_query("select * from employee", $connection);
while ($row = mysql_fetch_array($query)) {
echo "<b><a href='updatephp.php?update={$row['employee_id']}'>{$row['employee_name']}</a></b>";
echo "<br />";
}
?>
</div><?php
if (isset($_GET['update'])) {
$update = $_GET['update'];
$query1 = mysql_query("select * from employee where employee_id=$update", $connection);
while ($row1 = mysql_fetch_array($query1)) {
echo "<form class='form' method='get'>";
echo "<h2>Update Form</h2>";
echo "<hr/>";
echo"<input class='input' type='hidden' name='did' value='{$row1['employee_id']}' />";
echo "<br />";
echo "<label>" . "Name:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='dname' value='{$row1['employee_name']}' />";
echo "<br />";
echo "<label>" . "Email:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='demail' value='{$row1['employee_email']}' />";
echo "<br />";
echo "<label>" . "Mobile:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='dmobile' value='{$row1['employee_contact']}' />";
echo "<br />";
echo "<label>" . "Address:" . "</label>" . "<br />";
echo "<textarea rows='15' cols='15' name='daddress'>{$row1['employee_address']}";
echo "</textarea>";
echo "<br />";
echo "<input class='submit' type='submit' name='submit' value='update' />";
echo "</form>";
}
}
if (isset($_GET['submit'])) {
echo '<div class="form" id="form3"><br><br><br><br><br><br>
<Span>Data Updated Successfuly......!!</span></div>';
}
?>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</div><?php
mysql_close($connection);
?>
</body>
</html>

MySQL Code Segment:

Here is the MySQL code for creating database and table.


CREATE DATABASE company;
CREATE TABLE employee(
employee_id int(10) NOT NULL AUTO_INCREMENT,
employee_name varchar(255) NOT NULL,
employee_email varchar(255) NOT NULL,
employee_contact varchar(255) NOT NULL,
employee_address varchar(255) NOT NULL,
PRIMARY KEY (employee_id)
)

 CSS File: style.css


@import "http://fonts.googleapis.com/css?family=Droid+Serif";
/* Above line is used to import Google font style */
.maindiv{
margin:0 auto;
width:980px;
height:500px;
background:#fff;
padding-top:20px;
font-size:14px;
font-family:'Droid Serif',serif
}
.title{
width:100%;
height:70px;
text-shadow:2px 2px 2px #cfcfcf;
font-size:16px;
text-align:center;
font-family:'Droid Serif',serif
}
.divA{
width:70%;
float:left;
margin-top:30px
}
.form{
width:400px;
float:left;
background-color:#f0f8ff;
font-family:'Droid Serif',serif;
padding-left:30px
}
.divB{
width:100%;
height:100%;
background-color:#f0f8ff;
border:dashed 1px #999
}
.divD{
width:200px;
height:480px;
float:left;
background-color:#f0f8ff;
border-right:dashed 1px #999
}
#form3{
color:green;
font-weight:700
}
p{
font-weight:700;
text-align:center;
color:#5678C0;
font-size:18px;
text-shadow:2px 2px 2px #cfcfcf
}
form h2{
text-align:center;
text-shadow:2px 2px 2px #cfcfcf
}
textarea{
width:250px;
height:60px;
border-radius:1px;
box-shadow:0 0 1px 2px #123456;
margin-top:10px;
padding:5px 0;
border:none
}
.input{
width:250px;
height:15px;
border-radius:1px;
box-shadow:0 0 1px 2px #123456;
margin-top:10px;
padding:5px 0;
border:none;
margin-bottom:20px
}
.submit{
color:#fff;
border-radius:3px;
background:#1F8DD6;
padding:5px;
margin-top:40px;
border:none;
width:100px;
height:30px;
box-shadow:0 0 1px 2px #123456;
font-size:16px
}
a{
text-decoration:none;
font-size:16px;
margin:2px 0 0 30px;
padding:3px;
color:#1F8DD6
}
a:hover{
text-shadow:2px 2px 2px #cfcfcf;
font-size:18px
}
.clear{
clear:both
}

 Conclusion:

We showed you how Update command of MySQL is executed with PHP, soon we will be going to post DELETE  command with PHP. Keep following us to catch more updates.

Get more realated information through these blogs –