Comment insérer dans la base de données

Bonjour,
j'utilise le code php suivant pour l'insertion dans la base de donnée mais l'insertion ne s'effectue pas
voila le code

<?php
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$bdd->query('INSERT INTO goo(nom, prenom, group, note) VALUES ("hay","zamaz","03",20)');
?>
aidez moi s'il vous plais

3 réponses

  1. Bonjour,

    $pdo = new PDO('mysql:host=localhost;dbname=test','root',''); 
    
    catch (PDOException $exception) {
        echo $message= $exception->getMessage();
        die(); 
    }
    

    ensuite tu fais ton INSERT

    Cdt

    ps -> https://forums.commentcamarche.net/forum/affich-31392304-insertion-dans-la-base-ne-marche-pas

    Vérifie que ta requêtte SQL est bien formée ...

    [EDIT] oublié le " try "

    En informatique 99% des problèmes se situent entre le fauteuil et le clavier
    0
    1. c'était pas marché voila que ce qu'il m'affiche :

      Parse error: syntax error, unexpected 'catch' (T_CATCH) in C:\wamp\www\home (2).php on line 8
      0
      1. autant pour moi, j'ai oublié le TRY ^^

        try {
            $pdo = new PDO('mysql:host=localhost;dbname=test','root',''); 
        }
        catch (PDOException $exception) {
            echo $message= $exception->getMessage();
            die(); 
        }
        0
    2. ma table contiens
      id -> INT -> auto-incrément
      nom -> varchar
      prenom -> varchar
      group -> varchar
      note ->float

      apré l'execution de ce code :
      <?php
      try { $pdo = new PDO('mysql:host=localhost;dbname=test','root','');
      }

      catch (PDOException $exception) {
      echo $message= $exception->getMessage();
      die();
      }$bdd->query('INSERT INTO goo( ,nom, prenom, group, note) VALUES( ,"hay","zamaz","03",20)');
      ?>

      il m'affiche
      1/ Notice: Undefined variable: bdd in C:\wamp\www\home (2).php on line 12
      2/ Fatal error: Call to a member function query() on a non-object in C:\wamp\www\home (2).php on line 12

      je suis tellement perdu j'arrive pas a le résoudre
      0
      1. donne nous ton véritable code, pas de petits morceaux, utilises la coloration syntaxique ( en haut à droite, balise code PHP ) sinon nous n'allons pas nous en sortir ^^

        try {
            $pdo = new PDO('mysql:host=localhost;dbname=test','root',''); 
        }
        catch (PDOException $exception) {
            echo $message= $exception->getMessage();
            die(); 
        }
        $this->connexion_bd = $pdo;
        // traiter requete
        $this->data=$connexion_bd->query("XXXXXX");
        
        // si tu veux ( optionnel pour un INSERT )
        while( $this->data_vue = $this->data->fetch() ) { 
         ....... ton traitement pour chaque ligne retournée par ta requete
        }


        1/ Notice: Undefined variable: bdd in C:\wamp\www\home (2).php on line 12 

        normal, $bdd n'existe pas. Juste à ajouter $bdd = $pdo;

        2/ Fatal error: Call to a member function query() on a non-object in C:\wamp\www\home (2).php on line 12 

        C'est lié à la premiere erreur

        Pour savoir si l'ajout à bien fonctionné, ajoute

        echo  mysql_affected_rows());


        Cdt
        0