Pdo insert array dans une table
Résolu
Sinistrus
Messages postés
1010
Date d'inscription
Statut
Membre
Dernière intervention
-
Sinistrus Messages postés 1010 Date d'inscription Statut Membre Dernière intervention -
Sinistrus Messages postés 1010 Date d'inscription Statut Membre Dernière intervention -
Bonjour à tous,
Je rencontre un soucis au niveau de ce code, je voudrais insérer dans ma table une boucle mais je n'y arrive pas.
Voici le message d'erreur ($stmt->execute($row);) :
Pouvez-vous m'aider svp ?
Je rencontre un soucis au niveau de ce code, je voudrais insérer dans ma table une boucle mais je n'y arrive pas.
Voici le message d'erreur ($stmt->execute($row);) :
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /home/.../_generer.php:68 Stack trace: #0 /home/.../_generer.php(68): PDOStatement->execute(Array) #1 {main} thrown in /home/.../_generer.php on line 68
$data = array(); while ($ligne <= $_POST["txt_nbr_a"]) { $code_coupon = CodeCoupon()."-".CodeCoupon()."-".CodeCoupon()."-".CodeCoupon(); $code_coupon = !empty($code_coupon) ? $code_coupon : array(); $txt_hash = md5($code_coupon); $txt_hash = !empty($txt_hash) ? $txt_hash : array(); $txt_valeur = trim(strip_tags($_POST["txt_valeur_a"])); $txt_valeur = !empty($txt_valeur) ? "$txt_valeur" : 0; $txt_nature = trim(strip_tags($_POST["txt_nature_a"])); $txt_nature = !empty($txt_nature) ? "$txt_nature" : NULL; $txt_creation = date("Y-m-d"); $txt_expire = trim(strip_tags($_POST["txt_expire_a"])); $txt_expire = !empty($txt_expire) ? "$txt_expire" : NULL; $txt_expire = strftime(DATE_SQL, strtotime("+$txt_expire days")); echo "<tr><td><pre class='p-0 m-0'>".$code_coupon."</pre></td><td class='text-right'>".$txt_valeur."</td><td>".$txt_nature."</td><td class='text-right'>".$txt_expire."</tr>"; //add into array object while looping $data[] = array( "code_coupon" => $code_coupon, "valeur" => $txt_valeur, "nature" => $txt_nature, "date_creation" => $txt_creation, "date_expiration" => $txt_expire, "hash" => $txt_hash ); $ligne ++ ; } echo '</tbody></table></div>'; if(!empty($data)) { $stmt = $pdo->prepare("INSERT INTO tab_shop_coupon (code_coupon,valeur,nature,date_creation,date_expiration,hash) VALUES (?,?,?,?,?,?)"); try { $pdo->beginTransaction(); foreach ($data as $row) { $stmt->execute($row); } $pdo->commit(); }catch (Exception $e){ $pdo->rollback(); throw $e; } }
Pouvez-vous m'aider svp ?
Configuration: Windows / Chrome 91.0.4472.77
A voir également:
- Php pdo insert array
- Touche insert - Guide
- Easy php - Télécharger - Divers Web & Internet
- Disk boot failure insert system disk and press enter - Guide
- Expert php pinterest - Télécharger - Langages
- Activer touche insert sur ASUS UX32A - Forum Clavier
3 réponses
Bonjour,
je suppose que ce que tu envoies en POST sont des array ?
Dans ce cas, dans ta boucle, il faut utiliser un truc du genre
Dans le doute, si tu n'arrives pas à voir, fais un
et regarde ce qu'il contient....
je suppose que ce que tu envoies en POST sont des array ?
Dans ce cas, dans ta boucle, il faut utiliser un truc du genre
$txt_valeur = trim(strip_tags($_POST["txt_valeur_a"][$ligne]));
Dans le doute, si tu n'arrives pas à voir, fais un
var_dump($_POST);
et regarde ce qu'il contient....
Bonjour Jordane,
Les valeurs envoyées par POST sont uniques, c'est dans le fichier _generer.php que j'ai fais un While du code_coupon et du hash selon la valeur de txt_nbr_a...
Voici ce que le var_dump($_POST) me renvois :
index.php
_generer.php
Les valeurs envoyées par POST sont uniques, c'est dans le fichier _generer.php que j'ai fais un While du code_coupon et du hash selon la valeur de txt_nbr_a...
Voici ce que le var_dump($_POST) me renvois :
array(5) { ["txt_nbr_a"]=> string(1) "2" ["txt_valeur_a"]=> string(4) "3333" ["txt_nature_a"]=> string(4) "test" ["txt_expire_a"]=> string(1) "3" ["Generer_a"]=> string(0) "" }
index.php
<form name="form" action="_generer.php" method="POST" enctype="multipart/form-data"> <input type="number" id="txt_nbr_a" name="txt_nbr_a" value="1" required> <input type="text" id="txt_valeur_a" name="txt_valeur_a" required value=""> <input type="text" id="txt_nature_a" name="txt_nature_a" required value=""> <select id="txt_expire_a" name="txt_expire_a" required> <option selected></option> <option value="1">1 jour</option> <option value="3">3 jours</option> <option value="7">7 jours</option> <option value="15">15 jours</option> </select> <button class="btn btn-primary" name="Generer_a" type="submit">Générer</button> </form>
_generer.php
<?php function CodeCoupon( $longueur = 4 ) { $caracteres1 = "012"; $caracteres2 = "AbC"; $caracteres3 = "301"; $caracteres = $caracteres1.$caracteres2; $longueurMax = strlen( $caracteres ); $chaineAleatoire = ''; for ( $i = 0; $i < $longueur; $i++ ) {$chaineAleatoire .= $caracteres[ rand( 0, $longueurMax - 1 ) ];} return $chaineAleatoire; } if(isset($_POST['Generer_a'])){foreach($_POST as $var=>$val){if(!is_array($val)){$$var=strip_tags($val);}else{while(list($arvar,$arval)=each($val)){$$var[$arvar]=strip_tags($arval);}}} $txt_nbr = trim(strip_tags($_POST["txt_nbr_a"])); $txt_nbr = !empty($txt_nbr) ? "$txt_nbr" : NULL; $ligne = 1; $data = array(); while ($ligne <= $_POST["txt_nbr_a"]) { $code_coupon = CodeCoupon()."-".CodeCoupon()."-".CodeCoupon()."-".CodeCoupon(); $code_coupon = !empty($code_coupon) ? $code_coupon : array(); $txt_hash = md5($code_coupon); $txt_hash = !empty($txt_hash) ? $txt_hash : array(); $txt_valeur = trim(strip_tags($_POST["txt_valeur_a"])); $txt_valeur = !empty($txt_valeur) ? "$txt_valeur" : 0; $txt_nature = trim(strip_tags($_POST["txt_nature_a"])); $txt_nature = !empty($txt_nature) ? "$txt_nature" : NULL; $txt_creation = date("Y-m-d"); $txt_exp = trim(strip_tags($_POST["txt_expire_a"])); $txt_exp = !empty($txt_exp) ? "$txt_exp" : NULL; $txt_expire = strftime(DATE_SQL, strtotime("+$txt_exp days")); $data[] = array( "code_coupon" => $code_coupon, "valeur" => $txt_valeur, "nature" => $txt_nature, "date_creation" => $txt_creation, "date_expiration" => $txt_expire, "hash" => $txt_hash ); $ligne ++ ; } if(!empty($data)) { $stmt = $pdo->prepare("INSERT INTO tab_shop_coupons (code_coupon,valeur,nature,date_creation,date_expiration,hash) VALUES (?,?,?,?,?,?)"); var_dump($_POST)."<br>"; try { $pdo->beginTransaction(); foreach ($data as $row) { $stmt->execute($row); } $pdo->commit(); }catch (Exception $e){ $pdo->rollback(); throw $e; } } } ?>
Merci Jordane, tu m'as bien guidé.
if(!empty($data)) { $stmt = $pdo->prepare("INSERT INTO tab_shop_coupons (code_coupon,valeur,nature,date_creation,date_expiration,hash) VALUES (:code_coupon,:valeur,:nature,:date_creation,:date_expiration,:hash)"); try { $pdo->beginTransaction(); foreach ($data as $row) { $stmt->bindParam(':code_coupon', $row['code_coupon']); $stmt->bindParam(':valeur', $row['valeur']); $stmt->bindParam(':nature', $row['nature']); $stmt->bindParam(':date_creation', $row['date_creation']); $stmt->bindParam(':date_expiration', $row['date_expiration']); $stmt->bindParam(':hash', $row['hash']); $stmt->execute(); } $pdo->commit(); }catch (Exception $e){ $pdo->rollback(); throw $e; } }