PHP | MySQL UPDATE Query - GeeksforGeeks

01 Aug, 2021
The MySQL UPDATE query is used to update existing records in a table in a MySQL database.
It can be used to update one or more field at the same time.
It can be used to specify any condition using the WHERE clause.Syntax :The basic syntax of the Update Query is –Implementation of Where Update Query :Let us consider the following table “Data” with four columns ‘ID’, ‘FirstName’, ‘LastName’ and ‘Age’.To update the “Age” of a person whose “ID” is 201 in the “Data” table, we can use the following code :Update Query using Procedural Method :<?php$link = mysqli_connect("localhost", "root", "", "Mydb");  if($link === false){    die("ERROR: Could not connect. "                 . mysqli_connect_error());}  $sql = "UPDATE data SET Age='28' WHERE id=201";if(mysqli_query($link, $sql)){    echo "Record was updated successfully.";} else {    echo "ERROR: Could not able to execute $sql. "                             . mysqli_error($link);} mysqli_close($link);?>Output :Table After Updation –The output on Web Browser :Update Query using Object Oriented Method :<?php$mysqli = new mysqli("localhost", "root", "", "Mydb");  if($mysqli === false){    die("ERROR: Could not connect. "            . $mysqli->connect_error);}  $sql = "UPDATE data SET Age='28' WHERE id=201";if($mysqli->query($sql) === true){    echo "Records was updated successfully.";} else{    echo "ERROR: Could not able to execute $sql. "                                         . $mysqli->error;}$mysqli->close();?>Output :Table After Updation –The output on Web Browser :Update Query using PDO Method :<?phptry{    $pdo = new PDO("mysql:host=localhost;                    dbname=Mydb", "root", "");    $pdo->setAttribute(PDO::ATTR_ERRMODE,                         PDO::ERRMODE_EXCEPTION);} catch(PDOException $e){    die("ERROR: Could not connect. "                     . $e->getMessage());}  try{    $sql = "UPDATE data SET Age='28' WHERE id=201";    $pdo->exec($sql);    echo "Records was updated successfully.";} catch(PDOException $e){    die("ERROR: Could not able to execute $sql. "                                . $e->getMessage());}unset($pdo);?>Output :Table After Updation –The output on Web Browser :PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.My Personal Notes
arrow_drop_upSave
Syntax :The basic syntax of the Update Query is –
Let us consider the following table “Data” with four columns ‘ID’, ‘FirstName’, ‘LastName’ and ‘Age’.
To update the “Age” of a person whose “ID” is 201 in the “Data” table, we can use the following code :
Update Query using Procedural Method :
<?php$link = mysqli_connect("localhost", "root", "", "Mydb");  if($link === false){    die("ERROR: Could not connect. "                 . mysqli_connect_error());}  $sql = "UPDATE data SET Age='28' WHERE id=201";if(mysqli_query($link, $sql)){    echo "Record was updated successfully.";} else {    echo "ERROR: Could not able to execute $sql. "                             . mysqli_error($link);} mysqli_close($link);?>
Output :Table After Updation –
The output on Web Browser :
Update Query using Object Oriented Method :
<?php$mysqli = new mysqli("localhost", "root", "", "Mydb");  if($mysqli === false){    die("ERROR: Could not connect. "            . $mysqli->connect_error);}  $sql = "UPDATE data SET Age='28' WHERE id=201";if($mysqli->query($sql) === true){    echo "Records was updated successfully.";} else{    echo "ERROR: Could not able to execute $sql. "                                         . $mysqli->error;}$mysqli->close();?>
Output :Table After Updation –
The output on Web Browser :
Update Query using PDO Method :
<?phptry{    $pdo = new PDO("mysql:host=localhost;                    dbname=Mydb", "root", "");    $pdo->setAttribute(PDO::ATTR_ERRMODE,                         PDO::ERRMODE_EXCEPTION);} catch(PDOException $e){    die("ERROR: Could not connect. "                     . $e->getMessage());}  try{    $sql = "UPDATE data SET Age='28' WHERE id=201";    $pdo->exec($sql);    echo "Records was updated successfully.";} catch(PDOException $e){    die("ERROR: Could not able to execute $sql. "                                . $e->getMessage());}unset($pdo);?>
Output :Table After Updation –
The output on Web Browser :
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
mysql
PHP
SQL
Web Technologies
SQL
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
Comparing two dates in PHP
SQL | DDL, DQL, DML, DCL and TCL Commands
SQL | WITH clause
How to find Nth highest salary from a table
SQL | ALTER (RENAME)
SQL Trigger | Student Database