Update Data In a MySQL Table Using MySQLi in PHP

 Update Data In a MySQL Table Using MySQLi in PHP 

The UPDATE statement is used to update existing records in a table:

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

 

Notice:-  The WHERE clause specifies which record or records that should be updated. If omit the WHERE clause, all records will be updated!

 "MyGuests" table:

id

firstname

lastname

email

reg_date

1

John

Doe

john@example.com

2014-10-22 14:26:15

2

Mary

Moe

mary@example.com

2014-10-23 10:22:30

 

Example:- Update the record with id=2 in the "MyGuests" table:

Example (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if (mysqli_query($conn, $sql)) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

 Output:-

 

id

firstname

lastname

email

reg_date

1

John

Doe

john@example.com

2014-10-22 14:26:15

2

Mary

Doe

mary@example.com

2014-10-23 10:22:30

 ================

Post a Comment

0 Comments