Erreur sur programme PHP / UPDATE base de données MySQL

Résolu/Fermé
JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021 - 10 déc. 2020 à 00:29
JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021 - 12 déc. 2020 à 19:26
Bonjour,

J'ai une erreur dans mon programme PHP, pouvez vous m'expliquer?

Par le moyen d'un formulaire, j'envoie des données avec la methode POST, qui apparement arrive bien, mais le traitement provoque une alarme:

ECHEC : SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

Ci dessous le programme PHP.
Merci d'avance.

Configuration: Windows / Firefox 83.0


<?php
header("Content-Type: application/json; charset=UTF-8");

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);


$V0=$V1=$V2=$V3=$V4=$V5=$V6=$V7=$V8=$V9=$V10=$V11=$V12=
$V13=$V14=$V15=$V16=$V17=$V18=$V19=$V20=$V21=$V22=$V23="";

try{     
        
            function cleanData($dataToClean){
                $dataToClean = trim($dataToClean);
                $dataToClean = stripslashes($dataToClean);
                $dataCleaned = htmlspecialchars($dataToClean);
                return $dataCleaned;
            }
            
            $V0 = cleanData($_POST["IdM2"]);                                  
            $V1 = cleanData($_POST["refMagM2"]);
    echo 'variable id = '.$V0;
    echo $V1;            
            $V2 = cleanData($_POST["familleOutil"]);
            $V3 = cleanData($_POST["SousFamille"]);
            $V4 = cleanData($_POST["Matiere"]);
            $V5 = cleanData($_POST["NbreZ"]);
            $V6 = cleanData($_POST["Dia1"]);
            $V7 = cleanData($_POST["Long1"]);
            $V8 = cleanData($_POST["Dia2"]);
            $V9 = cleanData($_POST["Dia3"]);
            $V10 = cleanData($_POST["LongUtile"]);
            $V11 = cleanData($_POST["LongTotale"]);
            $V12 = cleanData($_POST["Caract1"]);
            $V13 = "Caract2";
            $V14 = cleanData($_POST["Fournisseur"]);
            $V15 = cleanData($_POST["Ref_Fournisseur"]);
            $V16 = cleanData($_POST["Prix"]);
            $V17 = cleanData($_POST["QteMini"]);
            $V18 = cleanData($_POST["CasEmploi1"]);
            $V19 = cleanData($_POST["CasEmploi2"]);
            $V20 = cleanData($_POST["CasEmploi3"]);
            $V21 = cleanData($_POST["CasEmploi4"]);
            $V22 = cleanData($_POST["CasEmploi5"]);
            $V23 = cleanData($_POST["CasEmploi6"]);

//--------------------------------------------------------------------------//
//connexion à la bdd
//--------------------------------------------------------------------------//
   require_once "cnxbdd.php";
    
                
    $requete = $connexion->prepare(
        "UPDATE cutting_tools_01 SET(SOUS_FAMILLE=:FAMILLE_OUTIL,MATIERE_OUTIL=:MATIERE_OUTIL,NBRE_Z=:NBRE_Z,
         DIA_1=:DIA_1,LONGUEUR_1=:LONGUEUR_1,DIA_2=:DIA_2,DIA_3:DIA_3,LONG_UTILE=:LONG_UTILE,LONG_TOTALE=:LONG_TOTALE,
         CARACT_1=:CARACT_1,CARACT_2=:CARACT_2,FOURNISSEUR=:FOURNISSEUR,REF_FOURNISSEUR=:REF_FOURNISSEUR,
         PRIX=:PRIX,QTE_MINI=:QTE_MINI,CAS_EMPLOI_1=:CAS_EMPLOI_1,CAS_EMPLOI_2=:CAS_EMPLOI_2,CAS_EMPLOI_3=:CAS_EMPLOI_3,
         CAS_EMPLOI_4=:CAS_EMPLOI_4,CAS_EMPLOI_5=:CAS_EMPLOI_5,CAS_EMPLOI_6=:CAS_EMPLOI_6)
         WHERE id=$V0");
                 
        $requete->bindParam(':id', $V0);    
        $requete->bindParam(':REF_MAG', $V1);
        $requete->bindParam(':FAMILLE_OUTIL', $V2);
        $requete->bindParam(':SOUS_FAMILLE', $V3);
        $requete->bindParam(':MATIERE_OUTIL', $V4);
        $requete->bindParam(':NBRE_Z', $V5);
        $requete->bindParam(':DIA_1', $V6);
        $requete->bindParam(':LONGUEUR_1', $V7);
        $requete->bindParam(':DIA_2', $V8);
        $requete->bindParam(':DIA_3', $V9);
        $requete->bindParam(':LONG_UTILE', $V10);
        $requete->bindParam(':LONG_TOTALE', $V11);    
        $requete->bindParam(':CARACT_1', $V12); 
        $requete->bindParam(':CARACT_2', $V13);      
        $requete->bindParam(':FOURNISSEUR', $V14);
        $requete->bindParam(':REF_FOURNISSEUR', $V15);
        $requete->bindParam(':PRIX', $V16); 
        $requete->bindParam(':QTE_MINI', $V17);                  
        $requete->bindParam(':CAS_EMPLOI_1', $V18);
        $requete->bindParam(':CAS_EMPLOI_2', $V19);         
        $requete->bindParam(':CAS_EMPLOI_3', $V20); 
        $requete->bindParam(':CAS_EMPLOI_4', $V21);                    
        $requete->bindParam(':CAS_EMPLOI_5', $V22);
        $requete->bindParam(':CAS_EMPLOI_6', $V23);


        // execute the query
        $requete->execute();           
}
            
catch(PDOException $e){
            echo 'ECHEC  : ' .$e->getMessage();
        }  

?>



A voir également:

6 réponses

jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
10 déc. 2020 à 07:27
Bonjour,

Déjà, comme le dit le message, tu as un nombre de paramètres dans ta requête (22) différents du nombre de variables que tu bind (24 )
En plus, tu as des parenthèses en trop dans ta requête... il faut les retirer !


1
JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021
10 déc. 2020 à 08:01
Bonjour Jordane,

1° )Quelle parenthèse en trop?

2°) Même en supprimant les 2 premiers paramètres , j' obtient toujours cette alarme.
ECHEC : SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

JCT
0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
Modifié le 10 déc. 2020 à 08:31
Les parenthèses qui entourent ton SET
 SET( ..... ) WHERE 


Et pour les paramètres... sans voir ce que tu as modifié impossible de te répondre.
Il faut bien que tu aies le même nombre entre tes bind et les variables de ta requête.

pour plus de lisibilité, je t'invite, au passage, à ne pas hésiter à faire un retour à la ligne devant chaque variable dans ta requête SQL
UPDATE cutting_tools_01 
  SET SOUS_FAMILLE=:FAMILLE_OUTIL
    ,MATIERE_OUTIL=:MATIERE_OUTIL
    ,NBRE_Z=:NBRE_Z
    ,DIA_1=:DIA_1
    ,LONGUEUR_1=:LONGUEUR_1
    ,DIA_2=:DIA_2
    ,DIA_3:DIA_3
    ,LONG_UTILE=:LONG_UTILE
    ,LONG_TOTALE=:LONG_TOTALE
    ,CARACT_1=:CARACT_1
    ,CARACT_2=:CARACT_2
    ,FOURNISSEUR=:FOURNISSEUR
    ,REF_FOURNISSEUR=:REF_FOURNISSEUR
    ,PRIX=:PRIX
    ,QTE_MINI=:QTE_MINI
    ,CAS_EMPLOI_1=:CAS_EMPLOI_1
    ,CAS_EMPLOI_2=:CAS_EMPLOI_2
    ,CAS_EMPLOI_3=:CAS_EMPLOI_3
    ,CAS_EMPLOI_4=:CAS_EMPLOI_4
    ,CAS_EMPLOI_5=:CAS_EMPLOI_5
    ,CAS_EMPLOI_6=:CAS_EMPLOI_6
WHERE id=$V0");


Dans ce que je vois.. ce sont ces deux là qui posent problème
$requete->bindParam(':REF_MAG', $V1);
$requete->bindParam(':SOUS_FAMILLE', $V3


je pense également que nommer tes variables V1 ..V2... n'est pas terrible.
En général on essai de donner un nom aux variables plus "parlant"
0
JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021
11 déc. 2020 à 00:39
Merci,
J'ai fait quelques corrections , notament dans le transfert des données POST, ça a amélioré la réponse.
Le nombre de variables liées semble correct (20), mais j'ai toujours cette alarme dans la réponse:

JSON
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Données brutes:
ECHEC : SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens




Maintenant le programme ressemble à ça:
<?php
header("Content-Type: application/json; charset=UTF-8");

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);


$V0=$V1=$V2=$V3=$V4=$V5=$V6=$V7=$V8=$V9=$V10=$V11=$V12=
$V13=$V14=$V15=$V16=$V17=$V18=$V19=$V20=$V21=$V22=$V23="";

try{     
        
            function cleanData($dataToClean){
                $dataToClean = trim($dataToClean);
                $dataToClean = stripslashes($dataToClean);
                $dataCleaned = htmlspecialchars($dataToClean);
                return $dataCleaned;
            }
            
            $V0 = cleanData($_POST["idM2"]);                                  
            $V1 = cleanData($_POST["refMagM2"]);           
            $V2 = cleanData($_POST["familleOutilM2"]);
            $V3 = cleanData($_POST["SousFamille"]);
            $V4 = cleanData($_POST["Matiere"]);
            $V5 = cleanData($_POST["NbreZ"]);
            $V6 = cleanData($_POST["Dia1"]);
            $V7 = cleanData($_POST["Long1"]);
            $V8 = cleanData($_POST["Dia2"]);
            $V9 = cleanData($_POST["Dia3"]);
            $V10 = cleanData($_POST["LongUtile"]);
            $V11 = cleanData($_POST["LongTotale"]);
            $V12 = cleanData($_POST["Caract1"]);

            $V14 = cleanData($_POST["Fournisseur"]);
            $V15 = cleanData($_POST["Ref_Fournisseur"]);
            $V16 = cleanData($_POST["Prix"]);
            $V17 = cleanData($_POST["QteMini"]);
            $V18 = cleanData($_POST["CasEmploi1"]);
            $V19 = cleanData($_POST["CasEmploi2"]);
            $V20 = cleanData($_POST["CasEmploi3"]);
            $V21 = cleanData($_POST["CasEmploi4"]);
            $V22 = cleanData($_POST["CasEmploi5"]);
            $V23 = cleanData($_POST["CasEmploi6"]);

//--------------------------------------------------------------------------//
//connexion à la bdd
//--------------------------------------------------------------------------//
   require_once "cnxbdd.php";
    
                
    $requete = $connexion->prepare(
        "UPDATE cutting_tools_01 
        SET SOUS_FAMILLE=:SOUS_FAMILLE
        ,MATIERE_OUTIL=:MATIERE_OUTIL
        ,NBRE_Z=:NBRE_Z
        ,DIA_1=:DIA_1
        ,LONGUEUR_1=:LONGUEUR_1
        ,DIA_2=:DIA_2
        ,DIA_3:DIA_3
        ,LONG_UTILE=:LONG_UTILE
        ,LONG_TOTALE=:LONG_TOTALE
        ,CARACT_1=:CARACT_1
        
        ,FOURNISSEUR=:FOURNISSEUR
        ,REF_FOURNISSEUR=:REF_FOURNISSEUR
        ,PRIX=:PRIX
        ,QTE_MINI=:QTE_MINI
        ,CAS_EMPLOI_1=:CAS_EMPLOI_1
        ,CAS_EMPLOI_2=:CAS_EMPLOI_2
        ,CAS_EMPLOI_3=:CAS_EMPLOI_3
        ,CAS_EMPLOI_4=:CAS_EMPLOI_4
        ,CAS_EMPLOI_5=:CAS_EMPLOI_5
        ,CAS_EMPLOI_6=:CAS_EMPLOI_6
         WHERE id=$V0");
                 
                 
        $requete->bindParam(':SOUS_FAMILLE', $V3);
        $requete->bindParam(':MATIERE_OUTIL', $V4);
        $requete->bindParam(':NBRE_Z', $V5);
        $requete->bindParam(':DIA_1', $V6);
        $requete->bindParam(':LONGUEUR_1', $V7);
        $requete->bindParam(':DIA_2', $V8);
        $requete->bindParam(':DIA_3', $V9);
        $requete->bindParam(':LONG_UTILE', $V10);
        $requete->bindParam(':LONG_TOTALE', $V11);    
        $requete->bindParam(':CARACT_1', $V12); 
        
        $requete->bindParam(':FOURNISSEUR', $V14);
        $requete->bindParam(':REF_FOURNISSEUR', $V15);
        $requete->bindParam(':PRIX', $V16); 
        $requete->bindParam(':QTE_MINI', $V17);                  
        $requete->bindParam(':CAS_EMPLOI_1', $V18);
        $requete->bindParam(':CAS_EMPLOI_2', $V19);         
        $requete->bindParam(':CAS_EMPLOI_3', $V20); 
        $requete->bindParam(':CAS_EMPLOI_4', $V21);                    
        $requete->bindParam(':CAS_EMPLOI_5', $V22);
        $requete->bindParam(':CAS_EMPLOI_6', $V23);

        // execute the query
        $requete->execute();           
}
            
catch(PDOException $e){
            echo 'ECHEC  : ' .$e->getMessage();
        }  

?>


0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
11 déc. 2020 à 06:46
Erreur dans la requête
DIA_3:DIA_3
0
JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021
12 déc. 2020 à 10:31
Ok , Merci
Pour cette partie c'est bon.
Il me reste juste un problème:

SyntaxError: JSON.parse: unexpected end of data at line 2 column 1 of the JSON data.

Qu'est ce qui provoque ça?

0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
12 déc. 2020 à 11:26
Déplie la "charge utile de la réponse" ..ça devrait donner un peu plus d'infos
0
JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021 > jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024
Modifié le 12 déc. 2020 à 14:03
La charge utile de la réponse est vide, nada...
0
yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 1 477 > JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021
12 déc. 2020 à 14:03
bonjour, que devrait-elle contenir?
0
JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021 > yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024
Modifié le 12 déc. 2020 à 14:21
Et bien je ne sais pas, je veux juste modifier le contenu de la database, je n'espère aucun "retour". C'est la première fois que je fais ça, j'ai le niveau "grand débutant" en programmation php... Lol...
Je veux juste retourner sur ma page principale après modification de la database. La modification de la database s'est faite à ce stade, mais la page d'erreur s'affiche.
JC
0
Utilisateur anonyme > JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021
12 déc. 2020 à 18:13
Hello,

Pourrais-tu nous poster le contenu de cnxbdd.php pour voir s'il n'y aurait pas une coquille ailleurs ? Pense à anonymiser les identifiants de connexion !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021
12 déc. 2020 à 18:44
voila le cnxbdd.php :

<?php
    $serveur = "localhost";
    $login = "root";
    $pass = "";

    $connexion = new PDO("mysql:host=$serveur;dbname=hydro", $login, $pass);
    $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

0
JC_8280 Messages postés 78 Date d'inscription jeudi 19 mars 2020 Statut Membre Dernière intervention 7 février 2021
12 déc. 2020 à 19:26
Merci à Tous pour votre aide!
0