0

I have a "Notice: undefined variable" error that I cannot solve:

Notice: Undefined variable: Nom in C:\wamp\www\cible_envoi.php on line 22

Could you please have a look at the code below and explain me what's wrong? Thanks!

cible_envoi.php:

<?php
try
{
    $bdd = new PDO('mysql:host=localhost;dbname=chansonniersduquebec.com', 'root', '');
}
catch (Exception $e)
{
    die('Erreur : ' . $e->getMessage());
}

$req = $bdd->prepare('INSERT INTO chansonniersduquebec.com (Nom) VALUES(?)');
$req->execute(array($_POST['Nom'])); // *** Line 22 ***

Incription.html:

<form id="Formulaire" name="Formulaire" method="post" action="cible_envoi.php">
  <label>
    <div align="center">Nom (votre nom d'artiste de préférence!)
      <span class="style1">*</span>
      <input type="text" name="Nom" id="Nom"/>
     </div>
     <p>&nbsp;</p>
   <label>
   ...
   <input type="submit" name="Soumettre" id="Soumettre" value="Soumettre" />
</form>
hakre
  • 193,403
  • 52
  • 435
  • 836
user1115535
  • 131
  • 2
  • 3
  • 9
  • 3
    Would be great if you can state the whole complete error message, because it should contain the filename and line the error was found. – Sven Oct 14 '12 at 22:19
  • what's the exact error message? @user1115535 – marknatividad Oct 14 '12 at 22:19
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – hakre Oct 14 '12 at 22:21
  • The complete error is the following: Notice: Undefined variable: Nom in C:\wamp\www\cible_envoi.php on line 22 Call Stack # Time Memory Function Location 1 0.0009 700112 {main}( ) ..\cible_envoi.php:0 – user1115535 Oct 14 '12 at 22:23
  • 1
    Have you posted the line 22? How can we know which variable is undefined if you only post part of your code? – BudwiseЯ Oct 14 '12 at 22:25
  • Sorry, it may not be well explained... The line 22 is the following: $req->execute(array($_POST['Nom'])); . I don't understand why "Nom" is not defined...? In http code it seems to be defined. – user1115535 Oct 14 '12 at 22:26
  • To find out whether that array index is defined or not, please take a look into `$_POST` fist: `var_dump($_POST);` will show you what is in there or not. It would be helpful, if you put the output of it as an edit to your question, too. If you see, I edited your question and reduced the code a little as well as marked line 22 and put the error message inside the question. Don't hide this important information in comments, you can edit your question to improve it. Also, take a look at the examples at [`PDOStatement::execute`](http://php.net/PDOStatement.execute). – hakre Oct 15 '12 at 06:58

1 Answers1

1

$bdd is out of scope.

     <?php
//Try this

    try
     {
        $bdd = new PDO('mysql:host=localhost;dbname=chansonniersduquebec.com', 'root', '');

         // Insertion du message à l'aide d'une requête préparée
          $req = $bdd->prepare('INSERT INTO chansonniersduquebec.com (Nom) VALUES(?)');

          $req->bindParam(1, $nom);

          $nom = $_POST['Nom'];

          $req->execute();
      }
      catch (Exception $e)
      {
        die('Erreur : ' . $e->getMessage());
      }



  ?>

or

<?php
//Try this
       $bdd = null;
    try
     {
        $bdd = new PDO('mysql:host=localhost;dbname=chansonniersduquebec.com', 'root', '');

      }
      catch (Exception $e)
      {
        die('Erreur : ' . $e->getMessage());
      }



         // Insertion du message à l'aide d'une requête préparée
          $req = $bdd->prepare('INSERT INTO chansonniersduquebec.com (Nom) VALUES(?)');

          $req->bindParam(1, $nom);

          $nom = $_POST['Nom'];

          $req->execute();


  ?>

please, also make your input name and ID one word (Styles joués).

Nwafor
  • 184
  • 6