Skip to content
PHP Logo

In what scenarios should an UPDATE query be executed in PHP?

UPDATE queries in PHP should be executed when you need to modify existing data in a database table. This can be useful when you want to update specific records based on certain conditions or user input. For example, you might want to update a user's information after they have submitted a form with new data.

php
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Update user information
$sql = "UPDATE users SET name='John Doe', email='john.doe@example.com' WHERE id=1";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>