Code ne s'execute pas.

Résolu
Bonjour,

J'ai une erreur (mon formulaire ne s'affiche pas) si je définis une variable reprenant la valeur d'une session. comme ceci :

        if (!isset($_SESSION['image']){
            $chemin_photo = "Dossiers_Clients/".$_POST['denomination']."/"."defaut.png";
        }
        else{
            $chemin_photo = $_SESSION['image'];
        }


Alors que si je fais:

$chemin_photo = ""; (ou n'importe quoi)


ça fonctionne. Alors que je fais tout ça seulement si mon formulaire est posté:
    if (isset($_POST['envoyer'])){


Je vous remercie de votre aide.

Ma fonction complète:

// ENREGISTREMENT DE LA FICHE 
function enregistrement(){
    include 'connexion.php';
// Si le formulaire est bien posté
    if (isset($_POST['envoyer'])){
        
        $denomination = htmlspecialchars(trim($_POST['denomination']));
        $contact = htmlspecialchars(trim($_POST['contact']));
        $adresse = htmlspecialchars(trim($_POST['adresse']));
        $cp = htmlspecialchars(trim($_POST['cp']));
        $ville = htmlspecialchars(trim($_POST['ville']));
        $email = htmlspecialchars(trim($_POST['email']));
        $telephone = htmlspecialchars(trim($_POST['telephone'])); 
        $portable = htmlspecialchars(trim($_POST['portable'])); 
        $commentaire = htmlspecialchars(trim($_POST['commentaire'])); 
           
        if (!isset($_SESSION['image']){
            $chemin_photo = "Dossiers_Clients/".$_POST['denomination']."/"."defaut.png";
        }
        else{
            $chemin_photo = $_SESSION['image'];
        }
            /*print_r($_SESSION);*/
        
        // VERIFICATION DE L'UNICITE DE LA DENOMINATION 
        $erreur = [];
        $maVar = $_POST['denomination'];
        $query = $bdd->query("SELECT COUNT(denomination) FROM fiche_client WHERE denomination = '$maVar'");
        $nombre = $query->fetch();
            if ($nombre[0] >0){
                $erreur['existe']  = "Cette dénomination existe déjà !";
                if(!empty($erreur)){

                    //Création de la session mon_tableau et du message d'erreur
                    if (!isset($_SESSION)){
                        session_start(); 
                    } 
                         $_SESSION['mon_tableau'] = array($denomination,$contact,$adresse,$cp,$ville,$email,$telephone,$portable,$commentaire);
                         $_SESSION['message'] = "La dénomination ".$denomination ." existe déjà !";
                    
                    header('location:index.php?page=ajoutClient');
                    
                }
            }
    else{

            #Requête préparée pour l'insertion
            $req = $bdd->prepare('INSERT INTO fiche_client(denomination, contact, adresse, cp, ville, email, telephone, portable,commentaire,chemin_photo) VALUES(:denomination, :contact, :adresse, :cp, :ville, :email, :telephone, :portable, :commentaire, :chemin_photo)');
            $req->execute(array(
                'denomination' => $denomination,
                'contact' => $contact,
                'adresse' => $adresse,
                'cp' => $cp,
                'ville' => $ville,
                'email' => $email,
                'telephone' => $telephone,
                'portable' => $portable,
                'commentaire' => $commentaire,
                'chemin_photo' => $chemin_photo
                ))or die(print_r($req->errorInfo(), TRUE));

            $req->closeCursor(); // Termine le traitement de la requête

        header('location:index.php?page=listeClients');
        
    }// else
    }//if
}//function


2 réponses

  1. Bonjour,
    Consermant wamp je ne l'aime pas parce qu'il a trop d'erreur, ce que j'aime le plus c'est WebLocal parce qu'il es puissant et na jamais recu d'erreur contrairement a wamp.
    le lien de téléchargement: http://ausea.eu.pn/
    1. Modérateur
      Bonjour,

      As tu bien démarré les sessions au debut de ton script ?
      Peux tu mettre le code :
      error_reporting(E_ALL);
      

      au début de ton code PHP pour faire apparaitre les erreurs php ?

      Et pour finir, pense à gérer les erreurs PDO.
      regarde ici :
      https://forums.commentcamarche.net/forum/affich-37584941-php-pdo-gerer-les-erreurs
      1. Oui j'ai fais ça au début:
        ini_set('display_errors','on');
        error_reporting(E_ALL);
        

        et mon code marche bien en local avec wamp server et la version 7 de php comme en distant la version est la même.

        J'ai donc essayé de cette façon:
        <?php
        ini_set('display_errors','on');
        error_reporting(E_ALL);
        
        // ENREGISTREMENT DE LA FICHE 
        function enregistrement(){
            include 'connexion.php';
        // Si le formulaire est bien posté
            if (isset($_POST['envoyer'])){
                
                $denomination = htmlspecialchars(trim($_POST['denomination']));
                $contact = htmlspecialchars(trim($_POST['contact']));
                $adresse = htmlspecialchars(trim($_POST['adresse']));
                $cp = htmlspecialchars(trim($_POST['cp']));
                $ville = htmlspecialchars(trim($_POST['ville']));
                $email = htmlspecialchars(trim($_POST['email']));
                $telephone = htmlspecialchars(trim($_POST['telephone'])); 
                $portable = htmlspecialchars(trim($_POST['portable'])); 
                $commentaire = htmlspecialchars(trim($_POST['commentaire'])); 
                   
                if (!isset($_SESSION['image']){
                    $chemin_photo = "Dossiers_Clients/".$_POST['denomination']."/"."defaut.png";
                }
                else{
                    $chemin_photo = $_SESSION['image'];
                }
                    /*print_r($_SESSION);*/
                
                // VERIFICATION DE L'UNICITE DE LA DENOMINATION 
                $erreur = [];
                $maVar = $_POST['denomination'];
                $query = $bdd->query("SELECT COUNT(denomination) FROM fiche_client WHERE denomination = '$maVar'");
                $nombre = $query->fetch();
                    if ($nombre[0] >0){
                        $erreur['existe']  = "Cette dénomination existe déjà !";
                        if(!empty($erreur)){
        
                            //Création de la session mon_tableau et du message d'erreur
                            if (!isset($_SESSION)){
                                session_start(); 
                            } 
                                 $_SESSION['mon_tableau'] = array($denomination,$contact,$adresse,$cp,$ville,$email,$telephone,$portable,$commentaire);
                                 $_SESSION['message'] = "La dénomination ".$denomination ." existe déjà !";
                            
                            header('location:index.php?page=ajoutClient');
                            
                        }
                    }
            else{
                try{
                    #Requête préparée pour l'insertion
                    $req = $bdd->prepare('INSERT INTO fiche_client(denomination, contact, adresse, cp, ville, email, telephone, portable,commentaire,chemin_photo) VALUES(:denomination, :contact, :adresse, :cp, :ville, :email, :telephone, :portable, :commentaire, :chemin_photo)');
                    $req->execute(array(
                        'denomination' => $denomination,
                        'contact' => $contact,
                        'adresse' => $adresse,
                        'cp' => $cp,
                        'ville' => $ville,
                        'email' => $email,
                        'telephone' => $telephone,
                        'portable' => $portable,
                        'commentaire' => $commentaire,
                        'chemin_photo' => $chemin_photo
                        ));
                        
                    }catch(Exception $e){
                        // en cas d'erreur :
                        echo " Erreur ! ".$e->getMessage();
                        echo " Les données : " ;
                        print_r($req);
                        }
        
                    $req->closeCursor(); // Termine le traitement de la requête
        
                header('location:index.php?page=listeClients');
                
            }// else
            }//if
        }//function
        ?>
        
      2. Ok j'ai trouvé: je n'affichais pas les erreurs sur mon formulaire et c'était juste une parenthèse non fermée.