Problème modifiaction PDO

Tandou Messages postés 2 Statut Membre -  
Firoxyd Messages postés 107 Statut Membre -
Bonjour,
je suis nouveau dans la programmation PDO,j'ai une table fournisseur dans laquelle je dois faire ajouter,modifier et supprimer,j'ai réussi a faire l'ajout et la supp et maina c'est la modif qui me pose problème puis que je n'arrive pas à voir d'ou bien l'erreur.
voici mon code
Page FournisseurAjout
 <?php
  $error = array();
  $nom='';
  $telephone='';
  $bp='';

 try

 {
  $db= new PDO('mysql:host=localhost;dbname=ges_vente','root','');

  if(!empty($_POST))
  {
   $nom=$_POST['nom'];
   $telephone=$_POST['telephone'];
   $bp=$_POST['bp'];


   {
     $select=$db->prepare("INSERT INTO fournisseur VALUES('','$nom','$telephone','$bp')");
     $select->execute();
    }

  }
 } 

   catch (Exception $e) 
      {

   echo $e->getTraceAsString();
   }

 if(isset($_GET['idsupp']))

   {
     $id=$_GET['idsupp'];
    
    $delete=$db->prepare("DELETE FROM fournisseur WHERE idfourni=$id" );
    $delete->execute();
   
     }


   if(isset($_GET['idmodif']))
 
{
  
    $nom=$_POST['nom'];
 $telephone=$_POST['telephone'];
 $bp=$_POST['bp'];


 $update=$db->prepare("UPDATE fournisseur SET nom='$nom',telephone='$telephone',bp='$bp' WHERE idfourni=$id");
 $update->execute();

}
  
?>

<!DOCTYPE <html>
<html>
 <head>
 <meta charset="utf-8"/>
  <link rel="stylesheet"  href="../style/style.css" />
 <title>Ajouter Fournisseur</title>
</head>
 <body>
     <h3>Ajouter un nouveau  Fournisseur </h3>
  <form method='POST' action=''>
   <label class=""> Nom: </label>
   <input name="nom"   type="text" size="20" value='<?=$nom?>'/>  
   <label class="">N° tél:</class>
   <input type="text" name="telephone" size="20" value='<?=$telephone?>'/>
   <label class="">BP:</label>
   <input type="text" name="bp" id="bp" size="20" value='<?=$bp?>'/>
   <input type="submit" name="submit" value="Ajouter"/>
   <input type="reset" value="Annuler">
  </form>


<table width="100%" border="2" cellspacing="1" cellpadding="5">
  <tr>
   <th>Nom</th>
   <th>N°Tél</th>
   <th>BP</th>
  </tr>
  
  <?php
   $sql = $db->prepare('SELECT * FROM fournisseur'); 
   $sql -> execute(array()); 
   
   
   
   $tab = array(); 
   while ($lignes = $sql -> fetch(PDO::FETCH_ASSOC)) 
   {
    echo '<tr>';
     $tab['idfourni'] = $lignes['idfourni']; 
     $tab['nomfourni'] = $lignes['nomfourni'];
     $tab['telfourni'] = $lignes['telfourni'];
     $tab['bpfourni'] = $lignes['bpfourni'];
     
   
    echo '<td>'.$tab['nomfourni'].'</td>';
    echo '<td>'.$tab['telfourni'].'</td>';
    echo '<td>'.$tab['bpfourni'].'</td>';
   
   
    
 echo '<td><a href=FournisseurModif.php?idmodif='.$tab['idfourni'].'>Modifier</a></td>'; 
       
echo '<td><a href=FournisseurAjout.php?idsupp='.$tab['idfourni'].'>Supprimer </a></td>'; 

 echo '</td>';
   
   }
   
    echo '</tr>';
    
   
   ?> 
 </table>

 </body>



</html> 


Voici la page de modification

Page FournisseurModif
<?php 
  session_start();
?>

 <?php
  
 try

 {
  $db= new PDO('mysql:host=localhost;dbname=ges_vente','root','');
  
  }   catch (Exception $e) 
      {

   echo $e->getTraceAsString();
   }
        
  if ($_GET['idmodif']){
         $id=$_GET['idmodif'];
  $select=$db->prepare("SELECT*FROM fournisseur WHERE idfourni=$id");
  $select->execute();
  $data=$select->fetch(PDO::FETCH_OBJ);    
}


 
?>

<!DOCTYPE <html>
<html>
 <head>
 <meta charset="utf-8"/>
  <link rel="stylesheet"  href="../style/style.css" />
 <title>Ajouter Fournisseur</title>
</head>
 <body>
  <h3>Modifier un  Fournisseur </h3>
  <form method='POST' action=''>
   <label class=""> Nom: </label>
   <input value="<?php echo $data->nomfourni?>"  name="nom"   type="text" size="20" /> <br/> 
   <label class="">N° tél:</class>
   <input value="<?php echo $data->telfourni?>"type="text" name="telephone" size="20"/> <br/>
   <label class="">BP:</label>
   <input value="<?php echo $data->bpfourni?>" type="text" name="bp" id="bp" size="20"/> <br/>
   <input type="submit" name="submit" value="Modifier"/>
   <input type="reset" value="Annuler">
  </form>
</body>
 </html>


SVP j'ai besoin d'aide.Merci

EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ICI

Merci d'y penser dans tes prochains messages.

4 réponses

  1. Firoxyd Messages postés 107 Statut Membre 47
     
    Salut,

    Apparemment tu n'initialises pas ta variable d'id dans le cas de l'update.

    Merci d'utiliser les balises de code, c'est difficile à lire en l'état.
    0
  2. Tandou Messages postés 2 Statut Membre
     
    Page FournisseurAjout

    
    <?php 
      session_start();
    ?>
    
     <?php
      $error = array();
      $nom='';
      $telephone='';
      $bp='';
    
     try
    
     {
      $db= new PDO('mysql:host=localhost;dbname=ges_vente','root','');
    
      if(!empty($_POST))
      {
       $nom=$_POST['nom'];
       $telephone=$_POST['telephone'];
       $bp=$_POST['bp'];
    
    
       {
         $select=$db->prepare("INSERT INTO fournisseur VALUES('','$nom','$telephone','$bp')");
         $select->execute();
        }
    
      }
     } 
    
       catch (Exception $e) 
          {
    
       echo $e->getTraceAsString();
       }
    
     if(isset($_GET['idsupp']))
    
       {
         $id=$_GET['idsupp'];
        
        $delete=$db->prepare("DELETE FROM fournisseur WHERE idfourni=$id" );
        $delete->execute();
       
         }
    
    
       if(isset($_GET['idmodif']))
     
    {
       $id=$_GET['idmodif'];
        $nom=$_POST['nom'];
     $telephone=$_POST['telephone'];
     $bp=$_POST['bp'];
    
    
     $update=$db->prepare("UPDATE fournisseur SET nom='$nom',telephone='$telephone',bp='$bp' WHERE idfourni=$id");
     $update->execute();
    
    }
      
    ?>
    
    <!DOCTYPE <html>
    <html>
     <head>
     <meta charset="utf-8"/>
      <link rel="stylesheet"  href="../style/style.css" />
     <title>Ajouter Fournisseur</title>
    </head>
     <body>
         <h3>Ajouter un nouveau  Fournisseur </h3>
      <form method='POST' action=''>
       <label class=""> Nom: </label>
       <input name="nom"   type="text" size="20" value='<?=$nom?>'/>  
       <label class="">N° tél:</class>
       <input type="text" name="telephone" size="20" value='<?=$telephone?>'/>
       <label class="">BP:</label>
       <input type="text" name="bp" id="bp" size="20" value='<?=$bp?>'/>
       <input type="submit" name="submit" value="Ajouter"/>
       <input type="reset" value="Annuler">
      </form>
    
    
    <table width="100%" border="2" cellspacing="1" cellpadding="5">
      <tr>
       <th>Nom</th>
       <th>N°Tél</th>
       <th>BP</th>
      </tr>
      
      <?php
       $sql = $db->prepare('SELECT * FROM fournisseur'); 
       $sql -> execute(array()); 
       
       
       
       $tab = array(); 
       while ($lignes = $sql -> fetch(PDO::FETCH_ASSOC)) 
       {
        echo '<tr>';
         $tab['idfourni'] = $lignes['idfourni']; 
         $tab['nomfourni'] = $lignes['nomfourni'];
         $tab['telfourni'] = $lignes['telfourni'];
         $tab['bpfourni'] = $lignes['bpfourni'];
         
       
        echo '<td>'.$tab['nomfourni'].'</td>';
        echo '<td>'.$tab['telfourni'].'</td>';
        echo '<td>'.$tab['bpfourni'].'</td>';
       
       
        
     echo '<td><a href=FournisseurModif.php?idmodif='.$tab['idfourni'].'>Modifier</a></td>'; 
           
    echo '<td><a href=FournisseurAjout.php?idsupp='.$tab['idfourni'].'>Supprimer </a></td>'; 
    
     echo '</td>';
       
       }
       
        echo '</tr>';
        
       
        
      
      ?> 
     </table>
    
     </body>
    
    
    
    </html>
    
    

    Page FournisseuModif

    
    <?php 
      session_start();
    ?>
    
     <?php
      
     try
    
     {
      $db= new PDO('mysql:host=localhost;dbname=ges_vente','root','');
      
      }   catch (Exception $e) 
          {
    
       echo $e->getTraceAsString();
       }
            
      if ($_GET['idmodif']){
             $id=$_GET['idmodif'];
      $select=$db->prepare("SELECT*FROM fournisseur WHERE idfourni=$id");
      $select->execute();
      $data=$select->fetch(PDO::FETCH_OBJ);    
    }
    
    
     
    ?>
    
    <!DOCTYPE <html>
    <html>
     <head>
     <meta charset="utf-8"/>
      <link rel="stylesheet"  href="../style/style.css" />
     <title>Modifier Fournisseur</title>
    </head>
     <body>
      <h3>Modifier un  Fournisseur </h3>
      <form method='POST' action=''>
       <label class=""> Nom: </label>
       <input value="<?php echo $data->nomfourni?>"  name="nom"   type="text" size="20" /> <br/> 
       <label class="">N° tél:</class>
       <input value="<?php echo $data->telfourni?>"type="text" name="telephone" size="20"/> <br/>
       <label class="">BP:</label>
       <input value="<?php echo $data->bpfourni?>" type="text" name="bp" id="bp" size="20"/> <br/>
       <input type="submit" name="submit" value="Modifier"/>
       <input type="reset" value="Annuler">
      </form>
    </body>
     </html>
    


    Même quand j'initialise ça ne marche pas merci

    EDIT : Ajout du LANGAGE dans les balises de code.
    Explications disponibles ICI

    Merci d'y penser dans tes prochains messages.
    0
  3. jordane45 Messages postés 30427 Date d'inscription   Statut Modérateur Dernière intervention   4 831
     
    Bonjour,

    Donc.. d'après ce que tu nous dis .. c'est l'update qui ne fonctionne pas ?

    Essayes avec ce code :
    //-------------------------------------------------------------------
    // Récupération "propre" des variables (à placer au début de ton script..
    //-------------------------------------------------------------------
    $id=!empty($_GET['idmodif'])?$_GET['idmodif']:NULL;
    $nom=isset($_POST['nom'])?$_POST['nom']:NULL;
    $telephone=isset($_POST['telephone'])?$_POST['telephone']:NULL;
    $bp=isset($_POST['bp'])?$_POST['bp']:NULL;
    
    //-------------------------------------------------------------------
    // Traitement UPDATE:
    //-------------------------------------------------------------------
    if($id){
     try{
       $sql = "UPDATE fournisseur 
                  SET nom=:nom
                     ,telephone=:telephone
                     ,bp=:bp 
              WHERE idfourni=:id";
    
       $params = array(":nom"=>$nom , ":telephone"=>$telephone , ":bp"=>$bp , ":id"=>$id);
    		
       $update=$db->prepare($sql);
       $update->execute($params);
       
     }catch(Exception $e){
       echo "<br>Erreur :". $e->getMessage();
       echo "<br>Requete:" .$sql ;
       echo "<br>Params :<br>";
       print_r($params);
     }
    
    }
    
    

    0
    1. jordane45 Messages postés 30427 Date d'inscription   Statut Modérateur Dernière intervention   4 831
       
      Par contre.. je viens de voir dans ton second code.....
      Peux tu m'expliquer pourquoi d'un côté .. le nom de tes champs semblent être du genre :
      "nomfourni" .. "telfourni" ... et de l'autre "nom", "telephone" ....
      .. ne te serais tu pas trompé dans ta requête d' UPDATE ?
      0
  4. Firoxyd Messages postés 107 Statut Membre 47
     
    Ne manquerait-il pas également un input hidden pour l'id dans le formulaire de modif ?
     <input value="<?php echo $id ?>"  name="idmodif"   type="hidden" 
    
    0