Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1615 connectés 

  FORUM HardWare.fr
  Programmation
  PHP

  Probléme avec fonction foreach

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Probléme avec fonction foreach

n°1662682
hppp
Serveur@home
Posté le 23-12-2007 à 21:13:11  profilanswer
 

Salut a tous
 
Je suis en trains de faire un script pour enregistre les commandes des clients mais j'ai un petit probléme avec la fonction foreach, quand j'ai plus de 5 produits à enregistré il me fait cette erreur:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Automne 130 gr (Pêche-poire)','1.5','10')' at line 1
mais si j'ai met 2 y a pas de probléme elle s'enregistre bien. Merci de votre aide
 
Voila mon code:
 

Code :
  1. foreach ($_POST['quantite'] as $reference => $quantite)
  2. {
  3.        $produits = query("SELECT * FROM lcg_pro_produits WHERE reference='$reference'" );
  4.    //boucle pour récuperer les info
  5.    while ($donnees_produits = mysql_fetch_array($produits))
  6.    {
  7.    // BDD lcg_pro_commandes_produits
  8.    $produits_id = $donnees_produits['id'];
  9.    $designation = $donnees_produits['designation'];
  10.    $prix = $donnees_produits['prix'];
  11.    $quantite = $quantite;
  12.    $reference = $reference;
  13.    $total_produit = $prix*$quantite;
  14.    $total = $total+$total_produit;
  15.  
  16.    query("INSERT INTO lcg_pro_commandes_produits VALUES ('','$produits_id','$id_commande','$reference','$designation','$prix','$quantite')" );
  17.  
  18.  
  19.    ?>
  20.    <tr>
  21.        <td class="td_general"><?php echo $reference; ?></td>
  22.        <td class="td_general"><?php echo $designation; ?></td>
  23.        <td class="td_general"><?php echo $prix; ?> €</td>
  24.    <td class="td_general"><?php echo $quantite; ?></td>
  25.        <td class="td_general"><?php echo $total_produit; ?> €</td>
  26.    </tr>
  27. <?php
  28. }
  29. }

mood
Publicité
Posté le 23-12-2007 à 21:13:11  profilanswer
 

n°1662745
NewsletTux
&lt;Insérez ici votre vie /&gt;
Posté le 24-12-2007 à 10:14:34  profilanswer
 

on pourrait avoir la ligne complète ?


---------------
NewsletTux - outil de mailing list en PHP MySQL
n°1662802
hppp
Serveur@home
Posté le 24-12-2007 à 13:29:38  profilanswer
 

voila toutes ma page:  
 
   

Code :
  1. <?php
  2.     if ($_GET['categorie']=="" )
  3. {
  4. //---------------------- Afficher Bon de commande ----------------------------------
  5. ?>
  6. <h2>Bon de Commande</h2>
  7.    <center><p>Remplissez ce bon de commande en indiquant les quantités que vous souhaitez pour chaque produits.
  8.    <br/>
  9.    si vous ne voulez pas commander un article, laissez la quantité à zéro.</p></center>
  10.  
  11.    <form action="index.php?page=passer-commande&amp;categorie=recapitulatif" method="post">
  12.    <center>
  13.    <table>
  14.    <tr>
  15.        <th>Référence</th>
  16.        <th>Désignation</th>
  17.        <th>Prix</th>
  18.    <th>Quantité</th>
  19.    </tr>
  20.    <?php
  21.    //recuperation des info
  22.    $produits = query("SELECT reference,designation,prix FROM lcg_pro_produits" );
  23.    //boucle pour récuperer les info
  24.    while ($donnees = mysql_fetch_array($produits) )
  25.  
  26.    {
  27. ?>
  28.    <tr>
  29.        <td class="td_general"><?php echo $donnees['reference']; ?></td>
  30.        <td class="td_general"><?php echo $donnees['designation']; ?></td>
  31.        <td class="td_general"><?php echo $donnees['prix']; ?></td>
  32. <td class="td_general"><input type="text" name="quantite[<?php echo  $donnees['reference']; ?>]" value="0" size="3" /></td>
  33.    </tr>
  34. <?php
  35. }
  36. mysql_close(); 
  37.    ?>
  38.    </table>
  39.       </center>
  40.    <br/>
  41.    <h2>Avant de valider votre Commande merci de bien verifier vos quantités car un retour en arriere n'est pas possible à moins de tout recommencer.</h2>
  42.    <br/>
  43.    <center><input type="submit" value="Validé votre Commande" /></center>
  44.    <?php
  45.    }
  46.  
  47.     //---------------------- Afficher recapitulatif commande ----------------------------------
  48.        elseif ($_GET['categorie']=="recapitulatif" )
  49. {
  50.    ?>
  51.    <div class="commande">
  52.    <h2>Vos informations Personnelles</h2>
  53.    <?php
  54.    //recuperation des info
  55.    $client = query('SELECT * FROM lcg_pro_clients WHERE id='.$_SESSION['id_client']);
  56.    //boucle pour récuperer les info
  57.    while ($donnees = mysql_fetch_array($client) )
  58.  
  59.    {
  60.    ?>
  61.    <center><table>
  62.    <tr>
  63.        <td class="td_general">N° de Compte Client :</td>
  64.        <td class="td_general"><?php echo $donnees['id']; ?></td>
  65.    </tr>
  66.    <tr>
  67.        <td class="td_general">Nom de l'entreprise :</td>
  68.        <td class="td_general"><?php echo $donnees['nom']; ?></td>
  69.    </tr>
  70.       <tr>
  71.        <td class="td_general">Contact :</td>
  72.        <td class="td_general"><?php echo $donnees['contact']; ?></td>
  73.    </tr>
  74.    <tr>
  75.        <td class="td_general">Adresse :</td>
  76.        <td class="td_general"><?php echo $donnees['adresse']; ?></td>
  77.    </tr>
  78.       <tr>
  79.        <td class="td_general">Adresse (2) :</td>
  80.        <td class="td_general"><?php echo $donnees['adresse2']; ?></td>
  81.    </tr>
  82.    <tr>
  83.        <td class="td_general">Code postal :</td>
  84.        <td class="td_general"><?php echo $donnees['code_postal']; ?></td>
  85.    </tr>
  86.       <tr>
  87.        <td class="td_general">Ville :</td>
  88.        <td class="td_general"><?php echo $donnees['ville']; ?></td>
  89.    </tr>
  90.    <tr>
  91.        <td class="td_general">Pays :</td>
  92.        <td class="td_general"><?php echo $donnees['pays']; ?></td>
  93.    </tr>
  94.       </tr>
  95.       <tr>
  96.        <td class="td_general">Votre adresse email :</td>
  97.        <td class="td_general"><?php echo $donnees['email']; ?></td>
  98.    </tr>
  99.    <tr>
  100.        <td class="td_general">Votre téléphone :</td>
  101.        <td class="td_general"><?php echo $donnees['tel']; ?></td>
  102.    </tr>
  103.       <tr>
  104.        <td class="td_general">Fax :</td>
  105.        <td class="td_general"><?php echo $donnees['fax']; ?></td>
  106.    </tr>
  107. </table></center>
  108. <?php
  109. }
  110. ?>
  111.    </div>
  112.  
  113.        <div class="commande">
  114.     <h2>Récapitulatif de Votre Commande</h2>
  115.     <form action="index.php?page=passer-commande&amp;categorie=validation" method="post">
  116.        <center>
  117.    <table>
  118.    <tr>
  119.        <th>Référence</th>
  120.        <th>Désignation</th>
  121.        <th>Prix</th>
  122.    <th>Quantité</th>
  123.    <th>Total</th>
  124.    </tr>
  125. <?php
  126. $total = 0;
  127. foreach ($_POST['quantite'] as $reference => $quantite)
  128. {
  129. if ($quantite >0)
  130. {
  131.    //recuperation des info
  132.    $produits = query("SELECT reference,designation,prix FROM lcg_pro_produits WHERE reference='$reference'" ) or die(mysql_error());
  133.    //boucle pour récuperer les info
  134.    while ($donnees = mysql_fetch_array($produits) )
  135.    {
  136. $prix = $donnees['prix'];
  137. $designation = $donnees['designation'];
  138. $total_produit=$prix*$quantite;
  139. $total=$total+$total_produit;
  140.  
  141. ?>
  142.    <tr>
  143.        <td class="td_general"><?php echo $reference; ?></td>
  144.        <td class="td_general"><?php echo $designation; ?></td>
  145.        <td class="td_general"><?php echo $prix; ?> €</td>
  146.    <td class="td_general"><?php echo $quantite; ?></td>
  147.        <td class="td_general"><?php echo $total_produit; ?> €</td>
  148.    </tr>
  149. <input type="hidden" name="quantite[<?php echo  $reference; ?>]" value="<?php echo  $quantite; ?>"/>
  150. <input type="hidden" name="prix" value="<?php echo  $total; ?>"/>
  151. <?php
  152. }
  153. }
  154. }
  155. mysql_close();
  156. ?>
  157.    </table>
  158.       </center>
  159. <p><center>Total de votre commande : <?php echo $total;?> € + Prix du Transport.</center></p>
  160. <p><center><input type="submit" value="Valider votre Commande" /></center></p>
  161.         </div>
  162.    
  163.        <div class="commande">
  164. <h2>Transporteur</h2>
  165. </div>
  166.    <?php
  167.    }
  168.        elseif ($_GET['categorie']=="validation" )
  169. {
  170. //---------------------- Validation de la commande ----------------------------------
  171.      
  172.        $id_client = $_SESSION['id_client'];
  173.        $client = query("SELECT * FROM lcg_pro_clients WHERE id='$id_client'" );
  174.    $donnees_clients = mysql_fetch_array($client);
  175.        // BDD lcg_pro_commandes
  176.        $date = date('Y-m-d H:i:s');
  177.    $client_id = $_SESSION['id_client'];
  178.    $statut = 0;
  179.    $livraison_nom = $donnees_clients['nom'];
  180.    $livraison_adr1 = $donnees_clients['adresse'];
  181.    $livraison_cp = $donnees_clients['code_postal'];
  182.    $livraison_ville = $donnees_clients['ville'];
  183.    $livraison_code_pays = $donnees_clients['code_pays'];
  184.    $livraison_etat = $donnees_clients['etat'];
  185.    $livraison_pays = $donnees_clients['pays'];
  186.    $total = $_POST['prix'];
  187.  
  188.    // ecriture dans la BDD lcg_pro_commandes
  189.    query("INSERT INTO lcg_pro_commandes VALUES('','".$date."','" . $client_id . "','" . $total . "','','','','','" . $statut . "','" . $livraison_nom . "','','" . $livraison_adr1 . "','','" . $livraison_cp . "','" . $livraison_ville . "','" . $livraison_code_pays . "','" . $livraison_etat . "','" . $livraison_pays. "')" );
  190.    ?>
  191.    <center>
  192.    <h2>Commande Bien enregistré</h2>
  193.        <table>
  194.    <tr>
  195.        <th>Référence</th>
  196.        <th>Désignation</th>
  197.        <th>Prix</th>
  198.    <th>Quantité</th>
  199.    <th>Total</th>
  200.    </tr>
  201. <?php
  202.  
  203.        $commande = query("SELECT * FROM lcg_pro_commandes WHERE dt_commande='$date'" );
  204.        $donnees_commande = mysql_fetch_array($commande);
  205.      
  206.        $id_commande = $donnees_commande['id'];
  207.      
  208.    foreach ($_POST['quantite'] as $reference => $quantite)
  209. {
  210.        $produits = query("SELECT * FROM lcg_pro_produits WHERE reference='$reference'" );
  211.    //boucle pour récuperer les info
  212.    while ($donnees_produits = mysql_fetch_array($produits))
  213.    {
  214.    // BDD lcg_pro_commandes_produits
  215.    $produits_id = $donnees_produits['id'];
  216.    $designation = $donnees_produits['designation'];
  217.    $prix = $donnees_produits['prix'];
  218.    $quantite = $quantite;
  219.    $reference = $reference;
  220.    $total_produit = $prix*$quantite;
  221.    $total = $total+$total_produit;
  222.  
  223.    query("INSERT INTO lcg_pro_commandes_produits VALUES ('','$produits_id','$id_commande','$reference','$designation','$prix','$quantite')" );
  224.  
  225.  
  226.    ?>
  227.    <tr>
  228.        <td class="td_general"><?php echo $reference; ?></td>
  229.        <td class="td_general"><?php echo $designation; ?></td>
  230.        <td class="td_general"><?php echo $prix; ?> €</td>
  231.    <td class="td_general"><?php echo $quantite; ?></td>
  232.        <td class="td_general"><?php echo $total_produit; ?> €</td>
  233.    </tr>
  234. <?php
  235. }
  236. }
  237. ?>
  238.    </table>
  239.       </center>
  240. <p><center>Total de votre commande : <?php echo $total;?> € + Prix du Transport.</center></p
  241. <p><a href='index.php?page=commandes-en-cours'>Retour</a></p>
  242. <?php
  243.    }
  244. mysql_close();
  245.    ?>

n°1662825
sircam
I Like Trains
Posté le 24-12-2007 à 15:49:27  profilanswer
 

http://forum.hardware.fr/hfr/Progr [...] m#t1655615
 

Citation :

Avez-vous bien testé vos queries SQL indépendemment du reste, p.e. dans PHPMyAdmin, avec des valeurs hard-codées si nécessaire?


---------------
Now Playing: {SYNTAX ERROR AT LINE 1210}
n°1662891
hppp
Serveur@home
Posté le 25-12-2007 à 12:33:06  profilanswer
 

c'est bon j'ai trouve mon probléme, mon script été bon mais le probleme c'est qu'il y avais une apostrophe dans le nom d'un produit se qu'il faisait buger la requette.


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  PHP

  Probléme avec fonction foreach

 

Sujets relatifs
JavaScript et PHP : problémescript php: probleme de recupération de données
Nombre de valeurs passées à une fonction, inconnue.Fonction avec parametre en char * et appel avec string
IE6 + problème UTF-8 ISO + javascriptfonction conditionnelle ?
Probleme urgent iframesProblème de requête d'insertion IIS/Access/ASP
Problème étrange d'affichage d'image avec gotoAndStopproblème avec fonction FOREACH
Plus de sujets relatifs à : Probléme avec fonction foreach


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR