Skip to content
PHP Logo

How can the issue of displaying outdated database values in a text field be resolved in PHP form processing?

Issue: To prevent displaying outdated database values in a text field in PHP form processing, you can retrieve the latest data from the database and populate the text field with the updated values before displaying the form.

php
<?php
// Connect to the database and retrieve the latest data
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

$sql = "SELECT * FROM table_name WHERE id = 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $value = $row['column_name'];
} else {
    $value = "";
}

$conn->close();
?>

<form method="post" action="process_form.php">
    <input type="text" name="field_name" value="<?php echo $value; ?>">
    <input type="submit" value="Submit">
</form>