[MySQL & PHP]Ecrire dans un base de donné

Bonjour,
<center><p>Ajouter votre adresse dans la BDD !</p></center> 
<center><strong>Votre E-mail :</strong><input type="text" name="mail"/></center><br/> 
<center><input type="submit" name="send" value="Ajouter Mail"/></center> 
<?php 
if(isset($_POST['send'])) 
{ 
$mail = $_POST['mail']; 
try { 
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; 
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '', $pdo_options); 
}catch(Exception $e) 
{ 
die('Erreur : '.$e->getMessage()); 
} 
$bdd=query('INSERT INTO 'testphp'('mail') VALUES (''.$mail.'')'); 
echo 'L\'adresse '.$mail.' a ete ajoute !'; 
} 
?>

Je n'arrive pas a faire fonctionner se code Merci


Norman FELTZ

2 réponses

  1. Salut !

    Tu as oublier la balise <form>, qui doit englober tout les champs de ton formulaire !
    Et auss, t'as oublier d'échaper des quotes !

    <form method="POST" action="#">
      <center>
        <p>Ajouter votre adresse dans la BDD !</p>
      </center>
    
      <center>
        <strong>Votre E-mail :</strong>
        <input type="text" name="mail"/>
      </center>
    
      <br/>
    
      <center>
        <input type="submit" name="send" value="Ajouter Mail"/>
      </center>
    </form>
    
    <?php 
    if(isset($_POST['send'])) { 
    
      $mail = $_POST['mail']; 
    
      try {
        $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
        $bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '', $pdo_options);
      }
      catch(Exception $e) {
        die('Erreur : '.$e->getMessage());
      }
    
      $bdd=query('INSERT INTO testphp(mail) VALUES ('.$mail.')');
      echo 'L\'adresse '.$mail.' a ete ajoute !';
    } 
    ?>
    1. Fatal error: Call to undefined function query() in C:\wamp\www\email.php on line 29
      Call Stack
      # Time Memory Function Location
      1 0.0017 372528 {main}( ) ..\email.php:0
    2. oups, c'est
      $bdd->query
    3. Marche pas

      ( ! ) Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@hotmail.fr)' at line 1' in C:\wamp\www\email.php on line 29
      ( ! ) PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@hotmail.fr)' at line 1 in C:\wamp\www\email.php on line 29
    4. !!!
      Au lieu de ...
      $bdd->query('INSERT INTO testphp(mail) VALUES ('.$mail.')');

      Essaye avec ...
      $req = $bdd->prepare('INSERT INTO testphp (mail) VALUES(:email)');
      $req->bindParam(':email',$mail,PDO::PARAM_STR);
      $req->execute();
    5. Merci mais mtn c'est cette page qui ne marche pas !
      <?php
      try {
      $bdd = new PDO('mysql:host=localhost;dbname=test', 'root', 'n.f67290');
      $reponse = $bdd->query('SELECT mail FROM testphp');
      while($donnees = $reponse->fetch())
      {
      $donnees['mail'] = $to;

      $subject = 'Test Script Norman';
      $message = '
      <html>
      <head>
      <title>Calendrier des anniversaires pour Août</title>
      </head>
      <body>
      <p>Le test du mail !</p>
      </body>
      </html>
      ';

      $headers = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

      $headers .= 'To: '.$to.'' . "\r\n";
      $headers .= 'From: <contact@oxocraft.ch>' . "\r\n";

      if(mail($to, $subject, $message, $headers))
      {
      echo 'Les mail ont ete envoye';
      }
      else
      {
      echo 'Les mail n\'ont pas pu etre envoye';
      }
      }
      $reponse->closeCursor();
      }catch(Exception $e)
      {
      die('Erreur : '.$e->getMessage());
      }
      ?>
  2. Si tu veux qu'on t'aide essai de nous aider aussi en nous donnant le maximum d'information et non pas en nous balançant ton code.

    Essai comme ça:
    $bdd=query("INSERT INTO testphp (mail) VALUES ('".$mail."')");

    As-tu une erreur qui s'affiche?
    Quel est le comportement de ton code après validation?