PHP : MySQL UPDATE Statement

The MySQL Update statement is used to update existing records in a database table. We can modify the records based on some conditions.

Syntax:

The following is the generic syntax of the UPDATE command to modify data in a MySQL table.
UPDATE table_nameSET column1 = value1, column2 = value2, ...WHERE condition;

  • We can update one or more fields altogether.
  • We can specify any condition using the WHERE clause.
  • We can update the values in a single table at a time.
<?php
/**
 * Create Database Connection
 */
 
$database_server = 'localhost';
$database_username = 'root';
$database_password = '';
$database_name = 'student';
 
// Create connection using mysqli_connect()
$conn = mysqli_connect($database_server, $database_username, $database_password, $database_name);
 
// If $conn is false, connection is failed
if (!$conn ) {
  die("Failed to connect to MySQL: " . mysqli_connect_error());
}
 
echo "Database Connected Successfully. <br />";
 
?>
<?php
/**
 * Update data in a Table
 */
 
require_once('db-connection.php');
 
// Mysql query to update record in a table
$mysql_query_statement = "UPDATE users SET name='Sanju' WHERE id=2";
 
if ($conn->query($mysql_query_statement) === TRUE) {
	echo "Record updated successfully.";
}
else {
	echo "Error updating record: " . $conn->error;
}
 
// Connection Close
mysqli_close($conn);
?>

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.