Devoid Design Devoid Design - 3 months ago 15
MySQL Question

Form Submission Doesn't Display MySQL Update (but update does execute change successfully)

I haven't gotten to preparing the statements yet. Want to get functionality down first.

I'm creating a page where the user can view the questions from the database and update them at the same time. The questions echo out into a text box. For each question/choices, I'm echoing out a form and submit button. When a change is made, the query runs successfully on submit and if you hit refresh in the browser, the changes will update. However, pressing the update button displays the old results rather than updating. What can I do here? Setting the header to refresh after form submission doesn't seem to work.


<?php include 'database.php'; include 'authenticate.php'; ?>
$questionText = $_POST['question'];
$qNum = $_POST['question_number'];
$query = "SELECT * FROM `questions`";
$result = $mysqli->query($query);
if (isset($_POST['update'])) {
$questionQuery = "UPDATE `questions` SET text = '$questionText' WHERE question_number = '$qNum'";
$mysqli->query($questionQuery) or die("Query failed: " . $mysqli->error.__LINE__);
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<?php while($row = $result->fetch_assoc()): ?>
<?php $questionNumber = $row['question_number']; ?>
<?php echo "<form action='update.php' method='post'>"; ?>
<?php echo $questionNumber; ?> <input type="text" name="question" value="<?php echo $row['text']; ?>" style="width: 75%" />
<input type="hidden" name="question_number" value="<?php echo $row['question_number']; ?>" /><br>
$query1 = "SELECT * FROM `choices` WHERE question_number = '$questionNumber'";
$result1 = $mysqli->query($query1);

//echo out choices right under each question
while ($row1 = $result1->fetch_assoc()) {
$text = $row1['text'];
if ($row1['is_correct'] == 1) {
echo "<input type='text' name='choice' value='$text' style='border: 4px solid lightgreen' />" . "<br>";
} else {
echo "<input type='text' name='choice' value='$text' style='border: 4px solid red' />" . "<br>";
<input type="submit" name="update" value="Update" />
<?php echo "</form>"; ?>
<?php echo "<br>"; ?>
<?php endwhile; ?>


This happens because you run a select before the update, so you will display the old values stored.

Move this line

  $result = $mysqli->query($query);

after the update block. :)