How can PHP be used to update data in a database and display the updated form afterwards?

To update data in a database using PHP, you can first establish a connection to the database, then execute an SQL UPDATE query to modify the desired data. After updating the data, you can retrieve the updated form from the database and display it to the user.

<?php
// Establish a connection to the database
$connection = mysqli_connect("localhost", "username", "password", "database_name");

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Update data in the database
$sql = "UPDATE table_name SET column_name = 'new_value' WHERE condition";
if (mysqli_query($connection, $sql)) {
    echo "Data updated successfully";
} else {
    echo "Error updating data: " . mysqli_error($connection);
}

// Retrieve the updated form from the database
$sql = "SELECT * FROM table_name WHERE condition";
$result = mysqli_query($connection, $sql);

// Display the updated form
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "Column 1: " . $row["column1"] . "<br>";
        echo "Column 2: " . $row["column2"] . "<br>";
        // Display other columns as needed
    }
} else {
    echo "0 results";
}

// Close the connection
mysqli_close($connection);
?>