We help IT Professionals succeed at work.
Get Started

Update from a foreach

Ahmad Jaber
Ahmad Jaber asked
on
62 Views
Last Modified: 2019-11-22
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
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE