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

nor67290 Messages postés 395 Date d'inscription   Statut Membre Dernière intervention   -  
nor67290 Messages postés 395 Date d'inscription   Statut Membre Dernière intervention   -
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
A voir également:

2 réponses

Utilisateur anonyme
 
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?
0
JooS Messages postés 2468 Date d'inscription   Statut Membre Dernière intervention   228
 
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 !';
} 
?>
0
nor67290 Messages postés 395 Date d'inscription   Statut Membre Dernière intervention   23
 
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
0
JooS Messages postés 2468 Date d'inscription   Statut Membre Dernière intervention   228
 
oups, c'est
$bdd->query
0
nor67290 Messages postés 395 Date d'inscription   Statut Membre Dernière intervention   23
 
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
0
JooS Messages postés 2468 Date d'inscription   Statut Membre Dernière intervention   228
 
!!!
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();
0
nor67290 Messages postés 395 Date d'inscription   Statut Membre Dernière intervention   23
 
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());
}
?>
0