How can a unique identifier like an id be used to identify and update specific records in a database using PHP?

To identify and update specific records in a database using PHP, you can use a unique identifier like an id. You can retrieve the id from the URL parameters or form data, then use it in your SQL query to target the specific record you want to update.

<?php
// Connect to the 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);
}

// Get the id from the URL parameters or form data
$id = $_GET['id'];

// Update specific record using the id
$sql = "UPDATE table_name SET column_name = 'new_value' WHERE id = $id";

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

$conn->close();
?>