Google News
logo
MySQL UPDATE Query
The MySQL UPDATE statement is used to update or modify  data of the existing MySQL records  within the database table .

MySQL query using the UPDATE statement and WHERE clause, after that we will execute this query through passing it to the PHP mysqli_query() function to update the tables records.
Syntax

UPDATE table_name SET column1=value, column2=value2,... WHERE column_name=some_value

Consider the following student_details table inside the demo database :

ID Name College Email ID Mobile
1V V Ramana ReddyS V Arts and Science Collegeinfo@freetimelearning.com9963666068
2ChantiVidhyalaya Degree Collegefreetimelearn@gmail.com9010023210
3RamanaS V Universityvvramanareddy9963@gmail.com9966463846
MySQLi UPDATE Query (Procedural)
<!DOCTYPE html>
<html>
<head>
	<title>PHP MySQL UPDATE Query</title>
</head>
<body>

<?php

	// Create connection
	$ftl_connect = mysqli_connect("localhost", "root", "", "ftl_db");
	// Check connection
	if (!$ftl_connect) {
		die("Connection failed: " . mysqli_connect_error());
	}
	
	$sql = "UPDATE student_details SET email='ramana@freetimelearn.com' WHERE stu_id=3";
	
	if (mysqli_query($ftl_connect, $sql)) {
		echo "Record has successfully updated..! ";
	} else {
		echo "Error updating record : " . mysqli_error($ftl_connect);
	}
	
	mysqli_close($ftl_connect);
	
?>

</body>
</html>
MySQLi UPDATE Query (Object-oriented)
<!DOCTYPE html>
<html>
<head>
	<title>PHP MySQL UPDATE Query</title>
</head>
<body>

<?php

	// Create connection
	$ftl_connect = mysqli_connect("localhost", "root", "", "ftl_db");
	// Check connection
	if ($ftl_connect->connect_error) {
		die("Connection failed: " . $ftl_connect->connect_error);
	} 
	
	$sql = "UPDATE student_details SET email='ramana@freetimelearn.com' WHERE stu_id=3";
	
	if ($ftl_connect->query($sql) === TRUE) {
		echo "Record successfully updated..!";
	} else {
		echo "Error updating record: " . $ftl_connect->error;
	}
	//Close Connection
	$ftl_connect->close();
	
?>
</body>
</html>
MySQL UPDATE Query (PDO)
<!DOCTYPE html>
<html>
<head>
	<title>PHP MySQL UPDATE Query</title>
</head>
<body>

<?php

	try {
		$ftl_connect = new PDO("mysql:host=localhost;dbname=ftl_db", "root", "");
		// set the PDO error mode to exception
		$ftl_connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	
		$sql = "UPDATE student_details SET email='ramana@freetimelearn.com' WHERE stu_id=3";
	
		// Prepare statement
		$ft_up = $ftl_connect->prepare($sql);
	
		// execute the query
		$ft_up->execute();
	
		// echo a message to say the UPDATE succeeded
		echo $ft_up->rowCount() . " Records has successfully updated ..! ";
		}
	catch(PDOException $e)
		{
		echo $sql . "<br>" . $e->getMessage();
		}
	//Close Connection
	$ftl_connect = null;
	
?>


</body>
</html>
Output :
The above examples update the record with stu_id=3 (email id) in the "student_details" table :