What is the correct way to display database values in an HTML form using PHP?

When displaying database values in an HTML form using PHP, you need to first retrieve the values from the database and then populate the form fields with those values. This can be done by fetching the data from the database using a query, storing the values in variables, and then echoing those variables as the values of the form fields.

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

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

// Retrieve data from the database
$sql = "SELECT * FROM table_name WHERE id = 1";
$result = mysqli_query($connection, $sql);

// Check if there is a result
if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $value1 = $row['column1'];
    $value2 = $row['column2'];
}

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

<form>
    <input type="text" name="field1" value="<?php echo $value1; ?>">
    <input type="text" name="field2" value="<?php echo $value2; ?>">
</form>