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.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 |
---|---|---|---|---|
1 | V V Ramana Reddy | S V Arts and Science College | info@freetimelearning.com | 9963666068 |
2 | Chanti | Vidhyalaya Degree College | freetimelearn@gmail.com | 9010023210 |
3 | Ramana | S V University | vvramanareddy9963@gmail.com | 9966463846 |
<!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>
<!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>
<!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>