MySQLi UPDATE
MySQL UPDATE statement is used to update existing data in a table.
Syntax
UPDATE Tablename SET column1 = value1, column2 = value2 [WHERE Clause];
These are the following ways to update data in Employee table -
Object Oriented PHP MySQLi Update Data
<?php
$conn = new mysqli('hostname', 'username', 'password', 'databasename');
//Check for connection error
if($conn->connect_error){
die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);
}
$update = "UPDATE employee SET emp_name = 'Smith' WHERE emp_id = '2' ";
if($conn->query($update)){
echo 'Data updated successfully';
}
?>
Procedural PHP MySQLi Update Data
<?php
$conn = mysqli_connect('hostname', 'username', 'password', 'databasename');
//Check for connection error
if(mysqli_connect_error()){
die("Error in DB connection: ".mysqli_connect_errno()." - ".mysqli_connect_error());
}
$update = "UPDATE employee SET emp_name = 'Smith' WHERE emp_id = '2' ";
if(mysqli_query($conn, $update)){
echo 'Data updated successfully';
}
?>
Prepared PHP MySQLi Update Statement
<?php
$conn = new mysqli('hostname', 'username', 'password', 'databasename');
//Check for connection error
if($conn->connect_error){
die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);
}
$empid = '2';
$query = "UPDATE employee SET emp_name = 'Smith' WHERE emp_id = ? ";
$update = $conn->prepare($query);
$update->bind_param('i', $empid);
$result = $update->execute();
if($result){
echo 'Data updated successfully';
}
?>