The code to alter a table goes on here.
<pre><?php
//connecting to host
mysql_connect("localhost", "root", "");
//Selecting a specific Database from available databases
mysql_select_db("Example");
//insert a row into a table
mysql_query("insert into mytable values(10,'lsfd','dfsdf')");
//update a specific row in a table
if(!mysql_query("update MyTable set slno=20 where email=10"))
{
echo 'Error'.mysql_error();
}
else
{
echo 'Update Successful';
}
?>
Here in this code we are performing two actions.
1) Insert a row into ‘MyTable’
2) Update a row in ‘MyTable’.
mysql_query("update MyTable set slno=20 where email=10")
Here this code firstly searches for a field called ‘slno’ in ‘MyTable’ and if a row with ‘slno’=20 is found then another field ‘email’ associated with that ‘slno’ will be updated to new value i.e.., 10
If you want to change the same field then you can make it happen by below code.
mysql_query("update MyTable set slno=20 where slno=10")
Here it searches for slno=20 and if that row is found then slno will be updated to slno=10.
On failure, it returns ‘ 0 ‘ with error which will be handled by ‘mysql_error()’ function. On success it returns ‘ 1 ‘.
Share this Story






12 Comments on "how to update field of a table in PHP"
Hi there, just became aware of your blog via Google,my site is http://www.chilggoooto.com
Your house is valueble for me. Thanks!…
Fantasticly written report, if only all bloggers provided the same posts as you, the net became a more beneficial place.
Thanks for another informative website. Where else may just I get that kind of info written in such an ideal approach? I have a project that I’m simply now running on, and I have been at the glance out for such information.
Highly descriptive article, I enjoyed that a lot. Will there be a part 2?
Great write-up. I’m a normal visitor of your site and appreciate you taking the time to maintain the nice site. I will be a frequent visitor for a long time.
The Twitter Auto post worked 1 time then quit working. Love this plugin though, it rocks! Thank you!
You completed a number of fine points there. I did a search on the subject matter and found mainly people will agree with your blog.
Hello, I like this page. I would like to get RSS Feed but something dont work. Any body help me?
Tip top stuff. I’ll eepxct more now.