probleme d'insertion de donnée dans la BDD

Par tedgael, il y a 6 ans


Bonjour,

Voila je rencontre un petit problème avec mon code.

Ce que je fais

Décrivez ici votre code ou ce que vous cherchez à faire

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="chat.css"> <title>MiniChat</title> </head> <style> form { text-align: center; } </style> <body> <form action="minichat_post.php" method="post"> <p> <label for ="pseudo">pseudo</label>: <input type="text" name="pseudo" id="pseudo"/><br/> <label for ="méssage">méssage</label>: <input type="text" name="méssage" id="méssage"/><br/> <input type="submit" value="envoyer"/> </p> </form> <?php //connexion bdd try { $bdd=new pdo('mysql:host=localhost;dbname=tchat;charset=utf8','root',''); } catch(exception $e) { die('erreur:'.$e->getmessage()); } //récupération des 10 derniers méssages $reponse=$bdd->query('select pseudo,messages from minichat order by id desc limit 0 , 10'); //on affiche les données while($données=$reponse->fetch()) { echo'<p><strong>'.htmlspecialchars($données['pseudo']).'</strong> : '.htmlspecialchars($données['messages']).'</p>'; } $reponse->closeCursor(); ?> </body> </html>

Ce que je veux

je cherche à remplir mon formulaire et enregistrer les données dans ma base de donnée

Ce que j'obtiens

j'arrive a remplir le formulaire mais rien n'est enregistré dans la BDD

6 réponses

Develop'On, il y a 6 ans

Bonjour,
Il faut que tu fasses un INSERT INTO pour enregitrer dans ta base.

tedgael, il y a 6 ans

Bonjour j'ai fais un insert into dans ma deuxieme page minichat_post.php mais rien ne change
<?php
//connexion bdd
try
{
$bdd=new pdo('mysql:host=localhost;dbname=tchat;charset=utf8','root','');
}
catch(exception $e)
{
die('erreur:'.$e->getmessage());
}

//insertion d'un méssage grace à une requète préparé
$req=$bdd->prepare('INSERT INTO minichat(pseudo,messages) VALUES(?,?)');
$req->execute(array($_POST['pseudo'],$_POST['messages']));

header('location:minichat.php');
?>

Carouge10, il y a 6 ans

Bonjour,

  • Activer les erreurs PDO (cf la doc)
  • C'est "Location" et non "location" (ne change rien en local mais en ligne oui)
  • D'ailleurs ne pas faire la redirection le temps de savoir ce qui ne va pas, sinon tu ne verras pas s'afficher les erreurs
Develop'On, il y a 6 ans

Voici un exemple d'INSERT INTO :

$req = $this->db->prepare('INSERT INTO table(name,lastname, id) VALUES (:name, :lastname, :id) ');
$req->bindValue(':name', $this->name, PDO::PARAM_STR);
$req->bindValue(':lastname', $this->lastname, PDO::PARAM_STR);
$req->bindValue(':id', $this->id, PDO::PARAM_INT);
return $req->execute();

tedgael, il y a 6 ans

merci Develop'On mon code fonctionne

Lartak, il y a 6 ans

Bonjour.

merci Develop'On mon code fonctionne

Si ton problème est résolu, indique le comme tel.