'Multiple update form but the code only get the last row as the value for the all row
function updateprs1($db,$it_no,$prs_qty,$prs_unit,$supp_id,$prs_price,$prs_no){
$stmt1 = $db->prepare("UPDATE prs_details SET prs_details.it_no = ?, prs_details.prs_qty = ?, prs_details.prs_unit= ?,prs_details.supp_id = ?, prs_details.prs_price= ? WHERE prs_details.prs_no = ? ");
$stmt1->execute(array($it_no,$prs_qty,$prs_unit,$supp_id,$prs_price,$prs_no));
}
Above is the function for the update
Below is the code to loop to the data inserted inside the include folder
foreach($n as $i => $total){
$upd=$prs->updateprs1($db,$_POST["it_no"][$i],$_POST["prs_qty"][$i],$_POST["prs_unit"][$i],$_POST["supp_id"][$i],$_POST["prs_price"][$i],$_POST["prs_no"][$i]);
}
When I use the var_dump the code seems fine (result of var_dump below)
Array ( [prs_no] => Array ( [0] => PRS-56V2YYBXYQ 1 => PRS-56V2YYBXYQ [2] => PRS-56V2YYBXYQ ) [it_no] => Array ( [0] => 9 1 => 9 [2] => 1 ) [prs_qty] => Array ( [0] => 1 1 => 2 [2] => 3 ) [prs_unit] => Array ( [0] => pcs 1 => box [2] => pcs ) [supp_id] => Array ( [0] => 1 1 => 10 [2] => 10 ) [prs_price] => Array ( [0] => 1000 1 => 2000 [2] => 3000 ) )
however when I execute the query the [2] from the array has been inserted for all of the row which resulted all of the value is the same as [2] rather than executing all of the arrays (image of result below)
below is my form
<form action="../include/prsupd.inc.php" method="POST" id="prs-form">
<?php if(isset($_GET['view']))
{
$prsupd=$prs->getInfoPrs($db,$_GET['view']);
for ($i=0; $i <= count($prsupd)-1; $i++) {
?>
<tr class="prs-item" data-id="">
<td class="align-middle p-1 text-center">
<button class="btn btn-sm btn-danger py-0" type="button" onclick="rem_item(this)"><i class="fa fa-times"></i></button>
</td>
<input type="hidden" step="any" class="text-right w-100 border-0" name="prs_no[]" value="<?php echo $_GET['view'] ?>"/>
<td class="align-middle p-1 text-center">
<select name="it_no[]" id="it_no" class="custom-select custom-select-sm rounded-0 select2" data-live-search="true" data-style="btn-primary">
<option value="" selected hidden>Please Select</option>
<?php
$sql = "SELECT item.it_no,item.particular,item.brand,item.status,item.itcat_id,item_category.itcat_name FROM item LEFT JOIN item_category ON item.itcat_id = item_category.itcat_id;";
$result = $db->query($sql);
while ($row=$result->fetch(PDO::FETCH_ASSOC))
{
?>
<option value="<?php echo $row['it_no']; ?>"><?php echo $row['particular']; ?> </option>
<?php
}
?>
</select>
</td>
<td class="align-middle p-1">
<input type="number" class="text-center w-100 border-0" name="prs_qty[]" value="<?php echo $prsupd[$i]['prs_qty']?>"/>
</td>
<td class="align-middle p-1">
<input type="text" class="text-center w-100 border-0" name="prs_unit[]" value=""/>
</td>
<td class="align-middle p-1 text-center">
<select name="supp_id[]" id="supp_id" class="custom-select custom-select-sm form-control" data-live-search="true" data-style="btn-primary">
<option value="" selected hidden>Please select</option>
<?php
$sql = "SELECT * FROM `supplier`;";
$result = $db->query($sql);
while ($row=$result->fetch(PDO::FETCH_ASSOC))
{
?>
<option value="<?php echo $row['supp_id']; ?>"><?php echo $row['supp_name']; ?> </option>
<?php
}
?>
</select>
</td>
<td class="align-middle p-1">
<input type="number" step="any" class="text-right w-100 border-0" name="prs_price[]" value=""/>
</td>
</tr>
<?php } }?>
</tbody>
<tfoot>
<tr class="bg-lightblue">
<tr>
<th class="p-1 text-right" colspan="6"><span><button class="btn btn btn-sm btn-flat btn-primary py-0 mx-1" type="button" id="add_row">Add Row</button></th>
</tr>
</tfoot>
</table>
<div class="card-footer">
<button class="btn btn-flat btn-primary" form="prs-form" id="add_btn">Save</button>
</div>
</tfoot>
</table>
</div>
</div>
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
