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 –
34 Replies to “Update Data in Database Using PHP”
Might an idea to update this from mysql to mysqli?
I kinda love this one, so easy and clean
Hello Jos,
MySqli is the updated version of MySql , so you need to follow certain instructions before you use it’s extensions.
Thanks & Regards,
FormGet Team.
Join Us On
Facebook : https://www.facebook.com/FormGet
Twitter : https://twitter.com/FormGetcom
Google Plus : https://plus.google.com/+Formget
thank u formget.com
thank u
Pleased with your appreciation. Keep reading our other blog posts.
Regards,
FormGet Team
can you give a simple explanation about this??
href=”updatephp.php?update={$row[’employee_id’]}”>{$row[’employee_name’]}
Hello Kim,
Yes , sure.
When you will click on to the name of a particular person then it will store the id of that person in update parameter of url.
If you will see in the complete code we are fetching the details of a particular person and showing it in front end .
$query = mysql_query(“select * from employee”, $connection);
while ($row = mysql_fetch_array($query)) {
echo “{$row[’employee_name’]}“;
echo “
“;
And as soon as anyone will click on to the name of that person, its id will get saved in to update parameter of url, which will be further used for update query.
$update = $_GET[‘update’];
$query1 = mysql_query(“select * from employee where employee_id=$update”, $connection);
Hope I have cleared your doubt.
Thanks & Regards,
Swapnil Gupta,
FormGet.com
Dear Admin,
I Am New in PHP.
I Copied your whole coding pasted as given files.And create DataBase and table according to you.
If i am not going wrong.So why his saying this ERROR.
Parse error: syntax error, unexpected ‘updatephp’ (T_STRING), expecting ‘,’ or ‘;’ in /opt/lampp/htdocs/MyFirstWebsite/updatephp.php on line 36
Hello Mohit,
The code has been updated, you can use it now.
If you wish, you can download the code, in that you will get all necessary files.
Thanks & Regards,
FormGet.com
Dear Admin,
what happen if the list name is more than 100 or 1000? Will it change to scroll mode automatically?
Thanks,
FLuke
why r u use ” did and dname etc” rather than d can we place any letter
if to do so what is the necessary actions to do
my code is : index.php
All Student List
Firstname
Lastname
Email
=================
and second page is : update.php
insert new record
student list
note : when i submit to form then error occur
Parse error: syntax error, unexpected ‘$fname’ (T_VARIABLE) in /opt/lampp/htdocs/test_studentprofile/update.php on line 16……..plz help me
Hello Deepak
syntax error, unexpected ‘$fname’ (T_VARIABLE) error will occur when semicolon or bracket missing.
Check your update.php
Hope I have solved your problem.
Thanks & Regards,
Swapnil Gupta,
FormGet.com
Hello admin, pl can you find what is the problem in this code>>>?? why the values are not updated>>
<input type="text" name="name" value="” />
<input type="text" name="email" value="” />
<input type="password" name="password" value="” />
<input type="text" name="mobile" value="”/>
<input type="text" name="address" value="”/>
Hello Trilok,
What exact problem you’re facing off please explain so we will help you.
Let me know
Regards,
FormGet Team
not comment in abd biz technolabs?
Hello sir,
I am new to php so i used your code for my operation but the above code of updatephp.php is unable to update my database. its displays the message of “Database Succesfully Updated” but the database isnt updated. i had slightly changed the components of my required table attributes but the code isnt working on my database. Help me asap
Hello sir,
I am new to php so i used your code for my operation but the above code of updatephp.php is unable to update my database. its displays the message of “Database Succesfully Updated” but the database isnt updated. i had slightly changed the components of my required table attributes but the code isnt working on my database. Help me asap
<?php
include 'connect_db.php';
$id=$_GET['id'];
$user=$_POST['username'];
$pass=$_POST['password'];
$up=mysqli_query($con,"UPDATE task SET username = '$user',password='$pass' WHERE t_id='".$id."'");
if($up)
{
echo "alert( ‘Successfully Updated’);window.location.href=’tasktable.php’;”;
}
else{
echo “alert( ‘Not Updated’);window.location.href=’tasktable.php’;”;
}
?>
i love the code.fit for beginners..kudoz
Hi,
I’m not able to get the full script. Been asked to wait 15 mins, it’s been longer.
Thanks.
Is this version also possible to get in MySqli? since i have to use this kind of form for school? anyone?
Messagehi. Admin can u tell me how update this record using PDO thanks
I tried replacing $_Get with $_POST in order to be able to enter more than 2000 characters into the textarea fields, but it now won’t display ANY of the MySQL data. Why doesn’t $_POST work if I change the method to “POST” instead of “Get”? Thanks!
You can certainly see your enthusiasm in the article you write.
The arena hopes for more passionate writers like you who are not afraid to mention how they believe.
All the time go after your heart.
Excellent beat ! I would like to apprentice while you amend your
website, how can i subscribe for a blog site? The account helped me a acceptable deal.
I had been tiny bit acquainted of this your broadcast provided bright clear
idea
Excellent post. I used to be checking constantly
this weblog and I’m impressed! Very useful info specifically the closing phase :
) I care for such information a lot. I used to
be looking for this particular information for a
very long time. Thank you and good luck.
You from Texas and don’t have a pair of boots?
Hey just ᴡanted tօ givе yοu а brief heads սp
and ⅼet yyou know a feᴡ of tһe images ɑren’t loading
correctly. Ι’m not sսre why Ƅut I think its ɑ linking issue.
I’ve trieԁ it іn two diffeгent browsers аnd botһ shnow thee same rеsults.
Feel free to surf t᧐ mmy web page – rumuspoker
What’s up mates, its fantastic article about cultureand fully
explained, keep it up all the time.
Hi Dear, are you in fact visiting this site daily, if so afterward you will definitely
take nice knowledge.
Hello, I wish for to subscribe for this web site to get newest updates, therefore where can i do it please assist.
Nicely written & done!
I egan writing a blog in the past few weeks aand observed many blogs simply rework old
ideas but add very little of value. It’s terrific to see
an enlightening write-up of some genuine value to myself and your other
readers.
It is going down on my list oof details I need to replicate as a new blogger.
Visitor engagement and mqterial value are king.
Some awesome ideas; you have unquestionably got on my list off blogs to watch!
Keep up the geat work!
Cheers,
Clarita
This is the right site for anyone who would like to
find out about this topic. You realize so much its almost hard to
argue with you (not that I personally would want to…HaHa).
You certainly put a brand new spin on a subject which has been discussed for many years.
Great stuff, just great!