Link to home
Start Free TrialLog in
Avatar of Jaber Ahmad
Jaber AhmadFlag for Côte d'Ivoire

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.

<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>

Open in new window


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();}
}

Open in new window


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"
  }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jaber Ahmad

ASKER

Thank you so much !
Yes indeed, I totally zapped.
Glad I could help :)