Tweet
Check out my previous two posts for convenience.
Displaying table of data from mysql database using php
Inserting data into mysql database using php
In SQL, the update command is usually written like this
UPDATE table_name SET col1='new_data' WHERE col2='present data'
Here is the php code for the update process.
<?php
$connection=mysql_connect("localhost","root","")
or die("Couldn't connect to the server");
$db=mysql_select_db("produce",$connection)
or die ("Couldn't select database");
$result=mysql_query("UPDATE fruit SET quantity='30kg' WHERE name='PINEAPPLE'");
$result=mysql_query("SELECT * FROM fruit");
echo "<table border='1'>";
echo "<tr>";
echo "<th>Name</th><th>Number</th><th>Quantity</th>";
echo "</tr>";
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>",$row['name'],"</td><td>",$row['number'],"
</td><td>",$row['quantity'],"</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($connection);
?>
Output before running the program :Name Number Quantity APPLE 1 5KG MANGO 2 10KG Orange 3 15kg PINEAPPLE 4 20KG
Output after running the program :Name Number Quantity APPLE 1 5KG MANGO 2 10KG Orange 3 15kg PINEAPPLE 4 30KG
This program is about how to update a record already present in the mysql database using php.