Witam!
Mam prosty formularz do edycji danych. Dane wczytują się do formularza, ale podczas aktualizacji aktualizowane są wszystkie rekordy wg ostatniej pozycji.
Po dodaniu
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:
<?php
$id = "-1";
if (isset($_GET['id'])) { $id = $_GET['id'];
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$material = $_POST['material'];
$cena = $_POST['cena'];
$sql_material = "UPDATE material_tab SET material=:material, cena=:cena WHERE zapotrzebowanie_id = $id";
$statement_mat = $pdo->prepare($sql_material);
foreach ($material as $key => $value) {
$statement_mat -> bindValue(':material', $material[$key], PDO::PARAM_STR);
$statement_mat -> bindValue(':cena', $cena[$key], PDO::PARAM_STR);
}
$statement_mat -> execute();
}
try {
$sql_zapo = "SELECT `zapotrzebowanie_id` FROM `zapotrzebowanie` WHERE `zapotrzebowanie_id` = $id LIMIT 1";
$statement = $pdo->query($sql_zapo);
$row_zapo = $statement->fetch(PDO::FETCH_ASSOC);
$sql_material = "SELECT * FROM material_tab WHERE zapotrzebowanie_id = $id";
$statement_material = $pdo->query($sql_material);
$rows_material = $statement_material->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $exception){
echo "Error: " . $exception->getMessage(); }
?>
<h2 class="center">Zapotrzebowanie
<?php echo $id; ?></h2> <form enctype="multipart/form-data" id="form1" name="form1" method="post" action="<?php echo $editFormAction; ?>">
<div class="formOverlay"> <table class="produkty" id="dataTable"> <th width="1%"> </th> <th width="59%">Produkt / usługa
</th> <th width="10%">PKWiU
</th> <th width="10%">Jedn. miary
</th> <th width="10%">Ilość
</th> <th width="10%">Cena
</th> <?php foreach ($rows_material as $material) { ?>
<td><input type="checkbox" name="chk" /></td> <td><input type="text" name="material[]" class="big_field required" value="<?php echo $material['material']; ?>" />
</td> <td><input type="text" name="pkwiu[]" class="small_field" value="<?php echo $material['pkwiu']; ?>" />
</td> <td><input type="text" name="jm[]" class="small_field" value="<?php echo $material['jm']; ?>" />
</td> <td><input type="text" name="ilosc[]" class="small_field" value="<?php echo $material['ilosc']; ?>" />
</td> <td><input type="text" name="cena[]" class="small_field" value="<?php echo $material['cena']; ?>" />
</td> <?php } ?>
<input class="button" type="submit" value="aktualizuj" /> <input type="hidden" name="zapotrzebowanie_id" value="<?php echo $row_zapo['zapotrzebowanie_id']; ?>" />
<input type="hidden" name="MM_update" value="form1" />