How can PHP be used to update existing data in a database based on specific conditions?

To update existing data in a database based on specific conditions, you can use an SQL UPDATE query in PHP. First, you need to establish a connection to your database using mysqli or PDO. Then, construct an SQL query that includes the conditions for updating the data. Finally, execute the query using the appropriate PHP function.

<?php
// Establish connection to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);

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

// Update data based on specific conditions
$sql = "UPDATE table_name SET column_name = 'new_value' WHERE condition = 'specific_condition'";
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

// Close connection
$conn->close();
?>