"PHP Update Data in MySQL" chapter gives information on how to update the data in Database. MySQL tables information can be upgraded by executing UPDATE statement of SQL with the PHP function mysql_query.
To update any table record, first the record has to be located using conditional clause. Primary key can also be used to match with any of the employee table record.
Syntax
UPDATE table_name SET field1=value1, field2=value2 [WHERE Clause]
Single/Multiple fields to be updated altogether.
Using WHERE clause, define the conditions.
Values in a single table can be updated at a time.
While updating the selected rows in a table,WHERE clause is the best choice.
Examples
MySQLi (Object-oriented)
[php]
<!DOCTYPE html>
<html>
<body>
<?php
$HostName = "localhost";
$UserName = "root";
$Password = "password";
$dbname="accounts";
// Create connection
$connection = new mysqli($HostName, $UserName, $Password, $dbname);
// Check connection
if ($connection->connect_error)
{
die("Connection failed: " . $connection->connect_error);
}
$sql = "UPDATE users2 SET Username='William', Password='password0', First='William', Last='Sarah' WHERE id=1";
if ($connection->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $connection->error;
}
$connection->close();
?>
</body>
</html>
[/php]
The output will be "Record updated successfully" by changing the records in phpmyadmin like below.