Update from a foreach

Ahmad Jaber
Ahmad Jaber used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hi there,

I think your problem is down to the names of the inputs you've used. In your PHP you have this:

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

Open in new window

But your form doesn't have any of those elements. Your elements are called NumId_AA, Libelle_FR & Libelle_AR, so your code should look like this:


if(isset($_POST['NumId_AA'])) {

$Num_AA = !empty($_POST['NumId_AA']) ? $_POST['NumId_AA'] : array();
$LIB_FR = !empty($_POST['Libelle_FR ']) ? $_POST['Libelle_FR '] : array();
$LIB_AR = !empty($_POST['Libelle_AR']) ? $_POST['Libelle_AR'] : array();

Open in new window

Also, in your Prepare statement, there are 3 placeholders (LIB_FR, LIB_AR & Num_AA) but you're only binding 2 of them. You need all three. Finally, you should run the bind the variables outside of the loop:

$UPD->bindParam("LIB_FR", $libFr);
$UPD->bindParam("LIB_AR", $libAr);
$UPD->bindParam("Num_AA", $num);

foreach($LIB_FR as $key => $value){
    $libFr = $value;
    $libAr = $LIB_AR[$key];
    $num = $Num_AA[$key];
    $UPD->execute();
}

Open in new window

Author

Commented:
Thank you so much !
Yes indeed, I totally zapped.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Glad I could help :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial