PHP Update Data in MySQL
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
look at the "Student" table:
ID name lastname rollno admsn_date
1 Ankit Bhardwaj 59 6/9/2016
2 Rahul Paswan 35 4/9/2016
The following examples update the record with id=2 in the "Student" table:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "data";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE Student SET lastname='Rajput' WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
After the record is updated, the table will look like this:
ID name lastname rollno admsn_date
1 Ankit Bhardwaj 58 6/9/2016
2 Rahul Rajput 35 4/9/2016
No comments