skip to Main Content

I have a php form that submits values into a mysql dB. It all works fine and the data updates in the dB, but when I submit the form the textarea doesnt show the update value. All fields show the original values fine before submit. Text inputs show the update values fine but not the textarea. If I refresh the page after submit it shows the updated value. Can anyone help with this please. I have searched a lot of a solution but no luck.

PHP CODE

$query = "SELECT about FROM table WHERE id = 1";
$stmt = $conn->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

// Set default values if no existing data

$about = $row['about'] ?? '';

// Initialize message variables
$message = "";
$updated = false;

// Step 3: Handle form submission (if form is submitted)
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $newAbout = $_POST['about'];

    // Check if anything has changed before updating
    if ($newAbout !== $about) {
        // Update record in the database
        $updateQuery = "UPDATE static_pages SET about = ? WHERE id = 1";
        $stmt = $conn->prepare($updateQuery);
        $stmt->bind_param("s", $newAbout);

        if ($stmt->execute()) {
            // On successful update, fetch the updated record
            $row['about'] = $newAbout;
            $message = "Record updated successfully!";
            $updated = true;
        } else {
            $message = "Error updating record: " . $stmt->error;
        }
        $stmt->close();
    } else {
        // If no changes made, show a message
        $message = "No changes were made.";
    }
}

Form Code

<form method="POST" action="">
<textarea id="about" name="about" rows="20"><?php echo htmlspecialchars($about); ?></textarea>     
<button class="button_blue">Update</button>
</form>

2

Answers


  1. Chosen as BEST ANSWER

    I should use <?= htmlspecialchars($row['about']) ?> not <?php echo htmlspecialchars($about); ?> for the textarea value.


  2. Problem

    The issue is that $about is set to the original database value at the start of the script. After the database is updated, the updated value is not reassigned to $about, so the textarea still displays the old value (the one you initially set to $about).


    Solution

    After successfully updating the database, you need to update the $about variable so that the textarea displays the new value.

    Updated PHP

    <?php
    $query = "SELECT about FROM table WHERE id = 1";
    $stmt = $conn->prepare($query);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    
    $about = $row['about'] ?? '';
    
    $message = "";
    $updated = false;
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $newAbout = $_POST['about'];
    
        if ($newAbout !== $about) {
            $updateQuery = "UPDATE static_pages SET about = ? WHERE id = 1";
            $stmt = $conn->prepare($updateQuery);
            $stmt->bind_param("s", $newAbout);
    
            if ($stmt->execute()) {
                // Update the $about variable with the new value
                $about = $newAbout;
                $message = "Record updated successfully!";
                $updated = true;
            } else {
                $message = "Error updating record: " . $stmt->error;
            }
            $stmt->close();
        } else {
            $message = "No changes were made.";
        }
    }
    ?>
    
    
    <form method="POST" action="">
        <textarea id="about" name="about" rows="20"><?php echo htmlspecialchars($about); ?></textarea>     
        <button class="button_blue">Update</button>
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search