Courses Job Ready Fresher Training AI for Class 7 to 12 Corporate Training Placements Tutorials
Free Learning Resources

IT Tutorials & Interview Prep

Free guides, interview Q&As, and job responsibility breakdowns — curated by industry veterans to help you crack MNC interviews

109+
Tutorial Articles
9
Topic Categories
100%
Free to Read
← Back to PHP

Updating Data in MySQL

PHP Last Updated: Oct 17, 2025

Updating Data in MySQL

Updating data in MySQL is very easy with the UPDATE statement. Let's assume we have this table where we need to update the lastname of the second employee.

employeeIDfirstnamelastnameemailjoining_date
1HarryBhaiharrybhai@bhai.com2025-10-17 14:54:58
2RohanDasrohanbhai@bhai.com2025-10-18 15:44:51

To update the lastname in the Employees table we will use the UPDATE command alongside SET command and WHERE command. The SET command assigns variables and the WHERE command specifies which record should be updated.

Note: If we don't use the WHERE clause, then all records will be updated.

Example:

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

// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE Employees SET lastname='Bhai' 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 something like this:

employeeIDfirstnamelastnameemailjoining_date
1HarryBhaiharrybhai@bhai.com2025-10-17 14:54:58
2RohanBhairohanbhai@bhai.com2025-10-18 15:44:51
Want to practice these scenarios in a real IT environment? Our 45-Day Internship Program gives you hands-on experience with Active Directory, Ticketing Tools, and Networking.
Join the Internship →