1

I have problems with understanding how to update multiple SQL data with my PDO query coming from several input fields. Here is my example:

My Input field generated by PDO row:

 <form action="" method="POST">
    <?php 
    $statement = $pdo->prepare("SELECT id, question, qid FROM questions WHERE questionaire_id = :questionaire_id");
    $result = $statement->execute(array(':questionaire_id' => $questionaire_id));
    $count = 1;
    while ($row = $statement->fetch()) {
    ?>
      <div class="text-secondard">Frage <?= $row['qid'] ?></div>
      <input type="text" name="question_id" value="<?= $row['id'] ?>" hidden>
      <input type="text" class="input mb-3" name="question" value="<?= $row['question'] ?>" required>
    <?php } ?>

    <input type="text" name="add_question" hidden>
    <button class="btn btn-primary"><i class="fas fa-plus"></i> Frage oder Anforderung erstellen</button>
  </form>

My Update Query:

$question = $_POST['question'];
$id = $_POST['question_id'];
$statement = $pdo->prepare("UPDATE questions SET question = :question WHERE id = :id");
$result = $statement->execute(array(':id' => $id, ':question' => $question));
ozahorulia
  • 9,798
  • 8
  • 48
  • 72
  • hi, its possible to update multiple rows in a single query with an update join or insert on conflict statement. You could also do a loop https://stackoverflow.com/questions/17886254/pdo-loop-for-updating-multiple-rows – jspcal Dec 20 '21 at 21:49
  • Because you are not using `[]` at the end of your field names, you are overwriting the submission values upon submit. I don't see why `$count` is declared. I found a comprehensive answer, then realised it was mine! https://stackoverflow.com/a/63431834/2943403 I really do drink my own kool-aid. – mickmackusa Dec 20 '21 at 21:55
  • OK Thank guys ;) – Mischa Mustermann Dec 20 '21 at 22:00

0 Answers0