Witam!
Mam prosty formularz do edycji danych. Dane wczytują się do formularza, ale podczas aktualizacji aktualizowane są wszystkie rekordy wg ostatniej pozycji.
Po dodaniu
  1. echo $material[$key].'<br />';
rekordy wypisują się prawidłowo ale rekordy w bazie aktualizują się według ostatniej pozycji w formularzu. Gdzie może być błąd?! Oto kod:

  1. <?php
  2. $id = "-1";
  3. if (isset($_GET['id'])) {
  4. $id = $_GET['id'];
  5. }
  6.  
  7. if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  8.  
  9. $material = $_POST['material'];
  10. $cena = $_POST['cena'];
  11. $sql_material = "UPDATE material_tab SET material=:material, cena=:cena WHERE zapotrzebowanie_id = $id";
  12. $statement_mat = $pdo->prepare($sql_material);
  13. foreach ($material as $key => $value) {
  14.  
  15. $statement_mat -> bindValue(':material', $material[$key], PDO::PARAM_STR);
  16. $statement_mat -> bindValue(':cena', $cena[$key], PDO::PARAM_STR);
  17.  
  18. }
  19. $statement_mat -> execute();
  20.  
  21. }
  22.  
  23. try {
  24. $sql_zapo = "SELECT `zapotrzebowanie_id` FROM `zapotrzebowanie` WHERE `zapotrzebowanie_id` = $id LIMIT 1";
  25. $statement = $pdo->query($sql_zapo);
  26. $row_zapo = $statement->fetch(PDO::FETCH_ASSOC);
  27.  
  28. $sql_material = "SELECT * FROM material_tab WHERE zapotrzebowanie_id = $id";
  29. $statement_material = $pdo->query($sql_material);
  30. $rows_material = $statement_material->fetchAll(PDO::FETCH_ASSOC);
  31.  
  32.  
  33. }catch(PDOException $exception){
  34. echo "Error: " . $exception->getMessage();
  35. }
  36.  
  37. ?>

  1. <h2 class="center">Zapotrzebowanie <?php echo $id; ?></h2>
  2. <form enctype="multipart/form-data" id="form1" name="form1" method="post" action="<?php echo $editFormAction; ?>">
  3. <div class="grid_12">
  4. <div class="formOverlay">
  5. <table class="produkty" id="dataTable">
  6. <tr>
  7. <th width="1%">&nbsp;</th>
  8. <th width="59%">Produkt / usługa</th>
  9. <th width="10%">PKWiU</th>
  10. <th width="10%">Jedn. miary</th>
  11. <th width="10%">Ilość</th>
  12. <th width="10%">Cena</th>
  13. </tr>
  14. <?php foreach ($rows_material as $material) { ?>
  15. <tr>
  16. <td><input type="checkbox" name="chk" /></td>
  17. <td><input type="text" name="material[]" class="big_field required" value="<?php echo $material['material']; ?>" /></td>
  18. <td><input type="text" name="pkwiu[]" class="small_field" value="<?php echo $material['pkwiu']; ?>" /></td>
  19. <td><input type="text" name="jm[]" class="small_field" value="<?php echo $material['jm']; ?>" /></td>
  20. <td><input type="text" name="ilosc[]" class="small_field" value="<?php echo $material['ilosc']; ?>" /></td>
  21. <td><input type="text" name="cena[]" class="small_field" value="<?php echo $material['cena']; ?>" /></td>
  22. </tr>
  23. <?php } ?>
  24. </table>
  25. </div>
  26. </div>
  27. <div class="clear"></div>
  28. <div class="grid_12">
  29. <input class="button" type="submit" value="aktualizuj" />
  30. </div>
  31. <input type="hidden" name="zapotrzebowanie_id" value="<?php echo $row_zapo['zapotrzebowanie_id']; ?>" />
  32. <input type="hidden" name="MM_update" value="form1" />
  33. </form>