Update a record in the mysql database using php

This program is about how to update a record already present in the mysql database using php.

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 :
NameNumberQuantity
APPLE15KG
MANGO210KG
Orange315kg
PINEAPPLE420KG
    
Output after running the program :
NameNumberQuantity
APPLE15KG
MANGO210KG
Orange315kg
PINEAPPLE430KG
 

Top