admin管理员组

文章数量:1122832

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>

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>
Share Improve this question edited Nov 23, 2024 at 6:27 user1823053 asked Nov 23, 2024 at 6:21 user1823053user1823053 691 gold badge2 silver badges8 bronze badges 7
  • 3 This code is written exactly to show the old value. If you want to show the new value, then you has to show it, not the old one. However, the best solution would be to do a redirect after POST, as it's required anyway. So your code will start working the way you want – Your Common Sense Commented Nov 23, 2024 at 6:31
  • Actually I think I see the problem. I should use <?= htmlspecialchars($row['about']) ?> not <?php echo htmlspecialchars($about); ?> – user1823053 Commented Nov 23, 2024 at 6:48
  • 1 Doing a redirect has nothing to do with solving the problem. – user1823053 Commented Nov 23, 2024 at 6:49
  • Can you please explain why I should do a redirect anyway – user1823053 Commented Nov 23, 2024 at 6:50
  • First UPDATE and then SELECT. But as mentioned before, it's best to do it in two separate requests, one POST and another GET. – Dharman Commented Nov 23, 2024 at 7:30
 |  Show 2 more comments

2 Answers 2

Reset to default 0

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

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>

本文标签: phpHow do I solve a problem where a textarea wont show updated value after form submitsStack Overflow