$_SESSION et MYSQL

Résolu
Bonjour,
J'ai un souci, quand je rentre le code suivant, les valeurs ne vont pas s'inscrire dans la base.

<?php
session_start();
include('connection.php');
echo $_SESSION['BAILLEURPHYSIQUE'][1]['prenombailleur'];

if ($_SESSION['TYPEBAILLEUR'] == 'OUI') {

$req_bailleur="INSERT INTO bailleur ('NOMBAILLEUR_1',
'PRENOMBAILLEUR_1',
'ADRESSEBAILLEUR_1',
'TELBAILLEUR_1',
'FAXBAILLEUR_1',
'VILLEBAILLEUR_1',
'CODEPOSTALBAILLEUR_1',
'MAILBAILLEUR_1'
)

VALUES ('$_SESSION[BAILLEURPHYSIQUE][1][nombailleur]',
'$_SESSION[BAILLEURPHYSIQUE][1][prenombailleur]',
'$_SESSION[BAILLEURPHYSIQUE][1][adressebailleur]',
'$_SESSION[BAILLEURPHYSIQUE][1][telbailleur]',
'$_SESSION[BAILLEURPHYSIQUE][1][faxbailleur]',
'$_SESSION[BAILLEURPHYSIQUE][1][villebailleur]',
'$_SESSION[BAILLEURPHYSIQUE][1][codepostalbailleur]',
'$_SESSION[BAILLEURPHYSIQUE][1][mailbailleur]'

)";

$result = mysql_query($req_bailleur);

};

?>

L'écho affiche bien la valeur voulu.
merci d'avance

15 réponses

  1. Bonjour,

    Le problème vient du fait que les tableaux ne sont pas interprétés dans les chaînes PHP.
    Il a dû te mettre un message d'erreur...

    La solution est soit de découper la chaîne en morceaux et de les concaténer (opérateur « . ») soit d'utiliser des accolades.

    Autre chose, dans ton « echo » tu mets bien des apostrophes droites autour des noms des champs du tableau, mais pas dans ta requête...

    Ce qui donne :
    $req_bailleur="INSERT INTO bailleur ('NOMBAILLEUR_1', 
    'PRENOMBAILLEUR_1', 
    'ADRESSEBAILLEUR_1', 
    'TELBAILLEUR_1', 
    'FAXBAILLEUR_1', 
    'VILLEBAILLEUR_1', 
    'CODEPOSTALBAILLEUR_1', 
    'MAILBAILLEUR_1' 
    ) 
    
    VALUES ('{$_SESSION['BAILLEURPHYSIQUE'][1]['nombailleur']}', 
    '{$_SESSION['BAILLEURPHYSIQUE'][1]['prenombailleur']}', 
    '{$_SESSION['BAILLEURPHYSIQUE'][1]['adressebailleur']}', 
    '{$_SESSION['BAILLEURPHYSIQUE'][1]['telbailleur']}', 
    '{$_SESSION['BAILLEURPHYSIQUE'][1]['faxbailleur']}', 
    '{$_SESSION['BAILLEURPHYSIQUE'][1]['villebailleur']}', 
    '{$_SESSION['BAILLEURPHYSIQUE'][1]['codepostalbailleur']}', 
    '{$_SESSION['BAILLEURPHYSIQUE'][1]['mailbailleur']}' 
    
    
    
    )"; 


    Xavier
    1. Bonjour et merci xavier j'ai fait les modifs mais je n'ai toujours rien qui s'inscrit en base.
      1. Bon, alors remplace ça

        $result = mysql_query($req_bailleur);

        par ça :

        echo $req_bailleur."<br />";
        $result = mysql_query($req_bailleur) or die (mysql_error()."<br />$req_bailleur");

        Relance, et dis-nous quel est le message d'erreur qui s'affiche.

        Fais aussi un
        echo $_SESSION['TYPEBAILLEUR']

        Pour être sûr qu'il contienne bien 'OUI'...

        Xavier
        1. bonjour xavier et merci de ton soutien voici le message d'erreur

          Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\test7.php on line 6

          par contre effectivement mon echo $_SESSION['TYPEBAILLEUR'] n'affiche rien
          1. re xavier, pour l'echo avais oublier de mettre le point virgule donc du cout voici le message d'erreur:

            OUIINSERT INTO bailleur ('NOMBAILLEUR_1', 'PRENOMBAILLEUR_1', 'ADRESSEBAILLEUR_1', 'TELBAILLEUR_1', 'FAXBAILLEUR_1', 'VILLEBAILLEUR_1', 'CODEPOSTALBAILLEUR_1', 'MAILBAILLEUR_1' ) VALUES ('DURAND', 'PIERRE', '', '', '', '', '', '' )
            Champ 'DURAND' inconnu dans field list
            INSERT INTO bailleur ('NOMBAILLEUR_1', 'PRENOMBAILLEUR_1', 'ADRESSEBAILLEUR_1', 'TELBAILLEUR_1', 'FAXBAILLEUR_1', 'VILLEBAILLEUR_1', 'CODEPOSTALBAILLEUR_1', 'MAILBAILLEUR_1' ) VALUES ('DURAND', 'PIERRE', '', '', '', '', '', '' )
            1. Bonjour,

              « Champ 'DURAND' inconnu dans field list »
              Ah...
              Apparemment, ton champ NOMBAILLEUR_1 est défini comme ne pouvant contenir qu'une liste prédéfinie de noms : tu ne peux pas y mettre n'importe quelle chaîne de caractère.
              Je pense qu'il y a un soucis dans la définition de ta table bailleur.
              Tu peux en donner le script de création ?
              (SHOW CREATE TABLE bailleur, de mémoire, en SQL)

              Xavier
              1. Laisse tomber ce message, c'est une c***rie :p
            2. ps: DURAND et PIERRE sont des données saisie dans mon formulaire
              1. re xavier

                j'ai passer ma requette sur phpmyadmin et voici le résultat

                $req_bailleur="INSERT INTO bailleur ('NOMBAILLEUR_1',
                'PRENOMBAILLEUR_1',
                'ADRESSEBAILLEUR_1',
                'TELBAILLEUR_1',
                'FAXBAILLEUR_1',
                'VILLEBAILLEUR_1',
                'CODEPOSTALBAILLEUR_1',
                'MAILBAILLEUR_1'
                )

                VALUES ("{$_SESSION["BAILLEURPHYSIQUE"][1]["nombailleur"]}",
                "{$_SESSION["BAILLEURPHYSIQUE"][1]["prenombailleur"]}",
                "{$_SESSION['BAILLEURPHYSIQUE'][1]['adressebailleur']}",
                "{$_SESSION['BAILLEURPHYSIQUE'][1]['telbailleur']}",
                "{$_SESSION['BAILLEURPHYSIQUE'][1]['faxbailleur']}",
                "{$_SESSION['BAILLEURPHYSIQUE'][1]['villebailleur']}",
                "{$_SESSION['BAILLEURPHYSIQUE'][1]['codepostalbailleur']}",
                "{$_SESSION['BAILLEURPHYSIQUE'][1]['mailbailleur']}"

                )";

                la requette fonctionne bien dans phpmyadmin

                mais maintenant voici le message d'erreur qui s'affiche:

                Parse error: syntax error, unexpected '{' in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\test7.php on line 22
                1. Ça c'est parce que dans ta dernière requête tu t'es mélangé dans les guillemets
                2. je ne voie pas le souci:

                  VALUES ("{$_SESSION["BAILLEURPHYSIQUE"][1]["nombailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["prenombailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["adressebailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["telbailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["faxbailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["villebailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["codepostalbailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["mailbailleur"]}"

                  )";
                3. $req_bailleur="INSERT
                  [...]
                  VALUES ("{$_

                  ce " ferme la chaîne initiée plus haut.
                4. la modif que tu me demande de faire et deja présente

                  $req_bailleur="INSERT INTO bailleur ('NOMBAILLEUR_1',
                  'PRENOMBAILLEUR_1',
                  'ADRESSEBAILLEUR_1',
                  'TELBAILLEUR_1',
                  'FAXBAILLEUR_1',
                  'VILLEBAILLEUR_1',
                  'CODEPOSTALBAILLEUR_1',
                  'MAILBAILLEUR_1'
                  )

                  VALUES("{$_SESSION["BAILLEURPHYSIQUE"][1]["nombailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["prenombailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["adressebailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["telbailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["faxbailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["villebailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["codepostalbailleur"]}",
                  "{$_SESSION["BAILLEURPHYSIQUE"][1]["mailbailleur"]}"

                  )";
              2. Oh attend, une idée comme ça...
                Quelles apostrophes tu utilises autour de 'DURAND' ? Celles qui sont sous le 4 ou celles qui sont sous le 7 ?
                1. Au niveau de ton php, essaie de copier-coller ce code-là :
                  $req_bailleur = "INSERT INTO bailleur (NOMBAILLEUR_1, 
                  PRENOMBAILLEUR_1, 
                  ADRESSEBAILLEUR_1, 
                  TELBAILLEUR_1, 
                  FAXBAILLEUR_1, 
                  VILLEBAILLEUR_1, 
                  CODEPOSTALBAILLEUR_1, 
                  MAILBAILLEUR_1 
                  ) 
                  
                  VALUES ('{$_SESSION['BAILLEURPHYSIQUE'][1]['nombailleur']}', 
                  '{$_SESSION['BAILLEURPHYSIQUE'][1]['prenombailleur']}', 
                  '{$_SESSION['BAILLEURPHYSIQUE'][1]['adressebailleur']}', 
                  '{$_SESSION['BAILLEURPHYSIQUE'][1]['telbailleur']}', 
                  '{$_SESSION['BAILLEURPHYSIQUE'][1]['faxbailleur']}', 
                  '{$_SESSION['BAILLEURPHYSIQUE'][1]['villebailleur']}', 
                  '{$_SESSION['BAILLEURPHYSIQUE'][1]['codepostalbailleur']}', 
                  '{$_SESSION['BAILLEURPHYSIQUE'][1]['mailbailleur']}' 
                  
                  )"; 
                  1. voici le message que j'ai quand je fait un copie coller de ton code:

                    OUIINSERT INTO bailleur (NOMBAILLEUR_1, PRENOMBAILLEUR_1, ADRESSEBAILLEUR_1, TELBAILLEUR_1, FAXBAILLEUR_1, VILLEBAILLEUR_1, CODEPOSTALBAILLEUR_1, MAILBAILLEUR_1 ) VALUES ('DURAND', 'PIERRE', '', '', '', '', '', '' )
                    Champ 'VILLEBAILLEUR_1' inconnu dans field list
                    INSERT INTO bailleur (NOMBAILLEUR_1, PRENOMBAILLEUR_1, ADRESSEBAILLEUR_1, TELBAILLEUR_1, FAXBAILLEUR_1, VILLEBAILLEUR_1, CODEPOSTALBAILLEUR_1, MAILBAILLEUR_1 ) VALUES ('DURAND', 'PIERRE', '', '', '', '', '', '' )
                    1. $req_bailleur = "INSERT INTO bailleur (NOMBAILLEUR_1,
                      PRENOMBAILLEUR_1,
                      ADRESSEBAILLEUR_1,
                      TELBAILLEUR_1,
                      FAXBAILLEUR_1,
                      VILLEBAILLEUR_1,
                      CODEPOSTALBAILLEUR_1,
                      MAILBAILLEUR_1
                      )

                      VALUES ('".$_SESSION['BAILLEURPHYSIQUE'][1]['nombailleur']."',
                      '".$_SESSION['BAILLEURPHYSIQUE'][1]['prenombailleur']."',
                      '".$_SESSION['BAILLEURPHYSIQUE'][1]['adressebailleur']."',
                      '".$_SESSION['BAILLEURPHYSIQUE'][1]['telbailleur']."',
                      '".$_SESSION['BAILLEURPHYSIQUE'][1]['faxbailleur']."',
                      '".$_SESSION['BAILLEURPHYSIQUE'][1]['villebailleur']."',
                      '".$_SESSION['BAILLEURPHYSIQUE'][1]['codepostalbailleur']."',
                      '".$_SESSION['BAILLEURPHYSIQUE'][1]['mailbailleur']."'

                      )";
                      1. Bonjour maka voici ce qui mes afficher:

                        OUIINSERT INTO bailleur (NOMBAILLEUR_1, PRENOMBAILLEUR_1, ADRESSEBAILLEUR_1, TELBAILLEUR_1, FAXBAILLEUR_1, VILLEBAILLEUR_1, CODEPOSTALBAILLEUR_1, MAILBAILLEUR_1 ) VALUES ('DURAND', 'PIERRE', '', '', '', '', '', '' )
                        Champ 'VILLEBAILLEUR_1' inconnu dans field list
                        INSERT INTO bailleur (NOMBAILLEUR_1, PRENOMBAILLEUR_1, ADRESSEBAILLEUR_1, TELBAILLEUR_1, FAXBAILLEUR_1, VILLEBAILLEUR_1, CODEPOSTALBAILLEUR_1, MAILBAILLEUR_1 ) VALUES ('DURAND', 'PIERRE', '', '', '', '', '', '' )
                        1. oooo je suis désolé..j'ai trop honte la...

                          VILLEBAILLEUR_1 et orthographier dans ma table VILLEBAILEUR_1

                          voila la raison... si non le dernier script de maka54 fonctionne ainsi que le dernier script de Reivax962*

                          merci à vous deux