Jaber Ahmad
asked on
Update from a foreach
Hello,
I have a little trouble updating my table from a loop.
Here is my html, my update code and finally my print_r($ _POST);
No update is done, I need your help please.
I have a little trouble updating my table from a loop.
Here is my html, my update code and finally my print_r($ _POST);
No update is done, I need your help please.
<form method="POST" action="_editer_produit.php" id="Form" name="Form">
<button type="submit" name="Editer_Libelle" id="Editer_Libelle" >Edit</button>
<?php try{$Stat = $pdo->query("SELECT * FROM tb_produits_test ORDER BY Libelle_FR"); $i = 0; while($data = $Stat->fetch(PDO::FETCH_ASSOC)){ ?>
<input type="hidden" name="NumId_AA[<?php echo $i;?>]" id="NumId_AA_<?php echo $i;?>" value="<?php echo $data["NumId"]; ?>">
<input type="text" id="Libelle_FR_<?php echo $i;?>" name="Libelle_FR[<?php echo $i;?>]" value="<?php echo $data["Libelle_FR"]; ?>">
<input type="text" id="Libelle_AR_<?php echo $i;?>" name="Libelle_AR[<?php echo $i;?>]" value="<?php echo $data["Libelle_AR"]; ?>">
<?php $i++; }}catch(PDOException $e){echo "<div class='alert alert-danger'>".$e->getMessage()."</div>";} ?>
</form>
if(isset($_POST['Num_AA'])) {
$Num_AA = !empty($_POST['Num_AA']) ? $_POST['Num_AA'] : array();
$LIB_FR = !empty($_POST['LIB_FR']) ? $_POST['LIB_FR'] : array();
$LIB_AR = !empty($_POST['LIB_AR']) ? $_POST['LIB_AR'] : array();
try{
$UPD = $pdo->prepare("UPDATE tb_produits_test SET Libelle_FR=:LIB_FR, Libelle_AR=:LIB_AR WHERE NumId=:Num_AA ");
foreach($LIB_FR as $key => $value){
$UPD->bindParam(":LIB_FR", $value);
$UPD->bindParam(":LIB_AR", $LIB_AR[$key]);
$UPD->execute();
}
}catch(PDOException $e){echo "<div class='alert alert-warning'>".$e->getMessage()."</div>"; die();}
}
array(4) {
["Editer_Libelle"]=>
string(0) ""
["Num_AA"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
["Libelle_FR"]=>
array(3) {
[0]=>
string(9) "libelle 1"
[1]=>
string(9) "libelle 2"
[2]=>
string(9) "libelle 3"
}
["Libelle_AR"]=>
array(3) {
[0]=>
string(3) "aaa"
[1]=>
string(3) "bbb"
[2]=>
string(3) "ccc"
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Glad I could help :)
ASKER
Yes indeed, I totally zapped.