requête de remplissage de plusieurs tables

Busima

Busima Le 24 mars 2019 à 23:24 (Édité le 25 mars 2019 à 08:19)

Salut. 
Je suis entrain de faire une migration de la procédure de réservation simple de produit vers la réservation à travers un panier. 
Sur le site, les clients avaient l'habitude de réserver un et seul produit par commande. Désormais, je veux maintenant donner la possibilité aux clients de mon site de pouvoir réserver plusieurs produits à la fois pour une même commande. 
Je suis maintenant au stade de la réservation des produits ajoutés au panier. Malheureusement je ne vois pas d'abord comment enregistrer dans ma base de données, tous les produits sélectionnés par le client (id des produits, noms des produit, prix...). Parce que j'avais l'habitude d'enregistrer le nom, le contact, l'email du client et l'identifiant "id" du produit que le client réservait dans une table "reservation". Je veux maintenant pouvoir enregistrer les informations du client avec les informations des produits contenus dans son panier pour savoir à quel client appartient la commande.
Ci-dessous le code de la page de réservation des produits que j'appelle "bigin_command.php":

<?php if(!isset($_SESSION)) 
{Session_start();} 
  
    error_reporting(E_ALL); 
    ini_set('display_errors', TRUE); 
    ini_set('display_startup_errors', TRUE); 
   // petit test pour voir si le panier existe 
  
    if(!isset($_SESSION['achats'])) { echo"<script language='javascript'> document.location.href='liste_pdt.php'; </script> "; } 
        /* session_start(); 
            if(isset($_GET["id"])) 
            { 
                $id=$_GET["id"]; 
                $_SESSION['id']=$id; 
            } 
        $new_id=$_SESSION['id']; */ 
          
    ?> 
  <html> 
        <head> 
            <meta charset="utf-8"> 
            <link rel="stylesheet" href="bootstrap/css/bootstrap.css" /> 
            <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" /> 
            <link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css"/> 
                <link rel="stylesheet" type="text/css" href="styl.css" /> 
                <link rel="stylesheet" href="menu_index.css" type ="text/css" /> 
                    <title>produits d'une catégorie</title> 
        </head> 
            <body> 
                <div id="bloc_page"> 
                        <?php include ('config.php');?> 
                        <?php include('header.php');?> 
                    <div class="container"> 
                                <div class="row">                             
                                    <form method="POST" action="end_command2.php"> 
                                        <div class="container">    
                                            <div class="row"> 
                                                <article class="col-md-12 col-sm-12 col-xs-12 ajuste"> 
                                                    <div class="panel panel-default"> 
                                                            <?php                                                                
                                            $total=0; $count=count($_SESSION['achats']); $tot=0; 
                                            if($count==0){  } 
  
                                                for($i=0;$i<$count;$i++){ 
                                            // on parcourt notre tableau session et on ne considère que les lignes encore active 
                                             if($_SESSION['achats'][$i]['statut']==0) { 
  
                                                $id=$_SESSION['achats'][$i]['id1']; // on recupère l'id 
                                                
                                                $qte=$_SESSION['achats'][$i]['qte']; // de même que la quantité 
                                                      
                                                require_once('config.php'); 
                                                $requete="select * from categories where id_Cat=$id"; 
                                                try 
                                                    { 
                                                        $reslt=$bdd->prepare($requete); 
                                                        $reslt->execute(); 
                                                        $lig3=$reslt->fetch(); $prix=number_format($lig3['prix'],0,'',''); 
                                                    } 
                                                    catch(PDOException $e) 
                                                        { 
                                                            // en cas d'erreur : 
                                                            echo " Erreur ! ".$e->getMessage(); 
                                                            print_r($reslt); 
                                                        }   
                                                /* <td height='34' class='lie4 zone esp'>{$lig3['nom']}</td>          <td align='center' class='lie4 zone'>$quantite_totale</td> 
                                                <input type='text' value='$qte' name='$i' size='5' class='li3'></td> */ 
                                                $tot=$lig3['prix']*$qte; 
                                                $total+=$tot; 
                                                echo" 
                                                <div class='col-md-8 col-sm-8 col-xs-8'> 
                                                    <tr></br> 
                                                          
                                                        <td align='center' class='lie zone'>- $qte {$lig3['nom']}(s) </td> dont le prix unitaire est 
                                                        <td align='center' class='lie4 zone'>$prix FCFA</td> = 
                                                        <td align='center' class='lie4 zone'>$tot FCFA</td> 
                                                    </tr> 
                                                </div> 
                                                <div class='col-md-4 col-sm-4 col-xs-4'></br> 
                                                    <td align='center' class='lie zone'><a href='#' onclick='effacer($id)' class='lie41'>Effacer</a></td> 
                                                </div> 
                                                </br></br></br>"; 
  
                                            // on incrémente le total 
  
                                            /* $tot=$lig3['prix']*$qte; 
                                             $total+=$tot; */ 
                                              } 
                                                  
                                            } 
                                              
                                            $prix1=number_format($total,0,'',' ');  
                                            echo'<tr> 
                                                  <td bgcolor="#0099CC" class="zone lie4 style7 style4">Prix Total à payer =  </td><td class="lie42 zone" align="center">'.$prix1.'FCFA</td> 
                                                </tr>                                         
                                                              
                                                              
                                                    </div> 
                                                </article> 
                                            </div> 
                                        </div>'; 
                                    ?>                                       
                                        <div class="container">    
                                            <div class="row"> 
                                                <article class="col-md-12 col-sm-12 col-xs-12 ajuste"> 
                                                    <div class="panel panel-default"> 
                                                            <div class="panel-heading"><h2>Donnez vos coordonnées pour la validation de votre réservation!</h2></div> 
                                                        <div class="panel-body"> 
                                                            <table> 
                                                                    <div class="col-md-4 col-sm-4 col-xs-4"> 
                                                                        <td> 
                                                                            <label>Nom</label><br/> 
                                                                            <input type="text" name="nomcli" size="120" class="form-control" required/> <br/> 
                                                                            <label>Adresse de livraison</label><br/> 
                                                                            <input type="text" name="adresselivraison" size="120"  class="form-control" required/> <br/> 
                                                                            <label>Contacts</label><br/> 
                                                                            <input type="tel" name="cell" size="120"  class="form-control" required/> </br> 
                                                                            <!--<input type="hidden" name="id" value="id" size="20"/> --> 
                                                                            <input type="submit" name="valider" value="valider" align="right"/> 
                                                                        </td> 
                                                                    </div> 
                                                                </tr> 
                                                                </center> 
                                                            </table> 
                                                        </div> 
                                                    </div> 
                                                </article> 
                                            </div> 
                                        </div> 
                                                                      
                                    </form> 
                                </div>    
                    </div> 
                        </br></br></br>                           
                                        <?php include('footer.php') ?> 
                </div> 
                        <script src="bootstrap/js/jquery.js"> </script> 
                        <script src="bootstrap/js/bootstrap.min.js"></script> 
            </body> 
    </html>


Je m'explique bien.
Au début, le client ne pouvait réserver qu'un seul produit par commande. Et maintenant je veux qu'il puisse réserver plusieurs produits par commande. De ce fait, je veux enregistrer les informations du client(idclient, nom, cell) dans une table "client", ensuite l'id du client doit aller dans la table commande(idcmd, datecmd, #idclient), enfin tous les produits que le client a réservé dans son panier doivent être enregistrés dans une table "produit(idp, nomp, qte, prix_u, prix_tot, #idcmd") de telle sorte que je puisse savoir pour chaque commande le client qui a passé la commande de tels et tels produits pour me faciliter la livraison des produits qu'il aura commandé.
Le remplissage de la table "client" se fait sans problème, seulement les tables "commande" et "produit" ne reçoivent pas leurs données pendant l'enregistrement.
Merci pour l'aide... 
Voici le code de la page de traitement "end_command.php"

<?php
    if(!isset($_SESSION))
    {session_start();}
 
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    require_once('config.php');
       $total=0; $count=count($_SESSION['achats']); $tot=0;
                                            if($count==0){  }
 
                                                for($i=0;$i<$count;$i++){
                                            // on parcourt notre tableau session et on ne considère que les lignes encore active
                                             if($_SESSION['achats'][$i]['statut']==0) {
 
                                                $id=$_SESSION['achats'][$i]['id1']; // on recupère l'id
                                               
                                                $qte=$_SESSION['achats'][$i]['qte']; // de même que la quantité
                                                     
                                                require_once('config.php');
                                                $requete="select * from categories where id_Cat=$id";
                                                try
                                                    {
                                                        $reslt=$bdd->prepare($requete);
                                                        $reslt->execute();
                                                        $lig3=$reslt->fetch(); $prix=$lig3['prix']; /* $prix=number_format($lig3['prix'],0,'','');  */
                                                    }
                                                    catch(PDOException $e)
                                                        {
                                                            // en cas d'erreur :
                                                            echo " Erreur ! ".$e->getMessage();
                                                            print_r($reslt);
                                                        }  
                                                $tot=$lig3['prix']*$qte;
                                                $total+=$tot;
                                                echo"
                                                <div class='col-md-8 col-sm-8 col-xs-8'>
                                                    <tr></br>
                                                         
                                                        <td align='center' class='lie zone'>- $qte {$lig3['nom']}(s) </td> dont le prix unitaire est
                                                        <td align='center' class='lie4 zone'>$prix FCFA</td> =
                                                        <td align='center' class='lie4 zone'>$tot FCFA</td>
                                                    </tr>
                                                </div>
                                                <div class='col-md-4 col-sm-4 col-xs-4'></br>
                                                    <td align='center' class='lie zone'><a href='#' onclick='effacer($id)' class='lie41'>Effacer</a></td>
                                                </div>
                                                </br></br></br>";
                                              }
                                                 
                                            }                                          
    ?>
  <html>
        <head>
            <meta charset="utf-8">
            <link rel="stylesheet" href="bootstrap/css/bootstrap.css" />
            <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
            <link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css"/>
                <link rel="stylesheet" type="text/css" href="styl.css" />
                <link rel="stylesheet" href="menu_index.css" type ="text/css" />
                <title>traitement de la reservation</title>
        </head>
            <body>
                <div id="bloc_page">
                        <?php include ('config.php');?>
                        <?php include('header.php');?>
                    <div class="container">
                                <div class="row">                            
                                    <form method="POST" action="">
                                        <div class="container">   
                                            <div class="row">
                                                <article class="col-md-12 col-sm-12 col-xs-12 ajuste">
                                                    <div class="panel panel-default">
                                                        <div class="panel-body">
                                                            <?php
                                                            $count=count($_SESSION['achats']);
                                                            if($count==0){  }
                                                            for($i=0;$i<$count;$i++){
                                                            if($_SESSION['achats'][$i]['statut']!=0)
                                                                {
                                                                    $id=$_SESSION['achats'][$i]['id1'];     // on recupère l'id
                                                                }
                                                            }
                                                                 
                                                            if (isset($_POST['valider']))
                                                                {
                                                                    $nomcli=$_POST['nomcli'];
                                                                    $adresselivraison=$_POST['adresselivraison'];
                                                                    $cell=$_POST['cell'];
                                                                    /* $id=$_SESSION['achats'][$i]['id1']; */
                                                                     
                                                                    echo "<center><h3><u> Vérifiez vos coordonnées svp </u></h3></center>";
                                                                    echo'<div class="col-md-3 col-sm-4 col-xs-4">
                                                                            <h3>Nom et prénom: </br></br>Adresse de livraison: </br></br>Téléphones: </br></h3>
                                                                        </div>
                                                                        <div class="col-md-9 col-sm-8 col-xs-8">
                                                                            <h3>'.$nomcli.'</br></br> ' .$adresselivraison.' </br></br>'.$cell.'</h3>
                                                                        </div>
                                                                         
                                                                    <form method="POST" action="">
                                                                        <input type="hidden" name="nomcli2" value="'.$nomcli.'" />
                                                                        <input type="hidden" name="adresselivraison2" value="'.$adresselivraison.'" />
                                                                        <input type="hidden" name="cell2" value="'.$cell.'" />
                                                                        <input type="hidden" name="id2" value="'.$id.'" /></br></br></br>
                                                                        <input class="annuler ch1" type="submit" name="annuler" value="Annuler" />
                                                                        <input class="ch2" type="submit" name="confirmer" value="Confirmer" />
                                                                    </form>';
                                                                }
                                                            ?>
                                                                    <?php
                                                                        if(isset($_POST['confirmer']))
                                                                        {  
                                                                            $nomcli2=isset($_POST['nomcli2']) ? $_POST['nomcli2'] : '' ;
                                                                            $adresselivraison2=isset($_POST['adresselivraison2']) ? $_POST['adresselivraison2'] : '';
                                                                            $cell2=isset($_POST['cell2']) ? $_POST['cell2'] : '';
                                                                            /* $id2=isset($_POST['id2']) ? $_POST['id2'] : ''; */
                                                                                 
                                                                                $req1 = "INSERT INTO cli(nomcli, adresselivraison, cell) VALUES(:nomcli2, :adresselivraison2, :cell2)";
                                                                                     
                                                                                $datas = array(':nomcli2'=>$nomcli2, ':adresselivraison2'=>$adresselivraison2, ':cell2'=>$cell2);         
                                                                                try
                                                                                    {
                                                                                        $requete = $bdd -> prepare($req1);
                                                                                        $requete->execute($datas) ; 
                                                                                    }catch(Exception $e)
                                                                                    {
                                                                                        // en cas d'erreur :
                                                                                        echo " Erreur ! ".$e->getMessage();
                                                                                        echo " Les datas : " ;
                                                                                        print_r($datas);
                                                                                    }
                                                                                    $last_id_in_cli = $bdd->lastInsertId();
                                                                            try
                                                                                {                                                          
                                                                                    $req2=$bdd->prepare('INSERT INTO commande(idcli, date_cmd) VALUES(:last_id_in_cli, NOW())');
                                                                                    $req2->execute(array(':last_id_in_cli'=>$last_id_in_cli));
                                                                                    $req2->closeCursor();
                                                                                     
                                                                                    $last_id_in_commande = $bdd->lastInsertId();
                                                                                     
                                                                                    /* <td align='center' class='lie zone'>- $qte {$lig3['nom']}(s) </td> dont le prix unitaire est
                                                                                    <td align='center' class='lie4 zone'>$prix FCFA</td> =
                                                                                    <td align='center' class='lie4 zone'>$tot FCFA</td> */
                                                         
                                $req3=$bdd->prepare('INSERT INTO produits_commandes(nomp, quantite, prixu, prixtotal, idcmd) VALUES(:'.$lig3['nom'].', :'.$qte.', :'.$prix.', :'.$tot.')');
                                                                                    $req3->execute(array(':nomp'=>$lig3['nom'],
                                                                                                        ':quantite'=>$qte,
                                                                                                        ':prixu'=>$prix,
                                                                                                        ':prixtotal'=>$tot,
                                                                                                        ':last_id_in_commande'=>$last_id_in_commande));
                                                                                                        $req3->closeCursor();
                                                                                     
                                                                                        echo'<h3> Réservation faite avec succès!</h3> </br><h3><a href="categories.php">Faire une autre réservation!</a></h3>';
                                                                                }catch(Exception $e)
                                                                                    {
                                                                                        // en cas d'erreur :
                                                                                        echo " Erreur ! ".$e->getMessage();
                                                                                        echo " Les datas : " ;
                                                                                        print_r($req2);
                                                                                    }
                                                                                     
                                                                                /* foreach($panier as $id_produit )
                                                                                    {
                                                                                        $req2->execute(array(':last_id_in_client'=>$last_id_in_client,
                                                                                                             ':id2'=>$id_produit ));
                                                                                    } */
 
                                                                        }else if(isset($_POST['annuler']))
                                                                                {
                                                                                    header('Location:index.php');
                                                                                }
                                                                      ?>
                                                        </div>
                                                    </div>
                                                </article>
                                            </div>
                                        </div>                                                                           
                                    </form>
                                </div>   
                    </div>
                        </br></br></br>                          
                                        <?php include('footer.php') ?>
                </div>
                        <script src="bootstrap/js/jquery.js"> </script>
                        <script src="bootstrap/js/bootstrap.min.js"></script>
            </body>
    </html>


Voici le message d'erreur qui s'affiche:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in C:\wamp\www\PROMOSHOP\end_command2.php on line 134
Vous devez être connecté pour poster une réponse. Se connecter ou Créer un compte