PDO Requêtes préparées

Fermé
fab1105 Messages postés 114 Date d'inscription mardi 4 août 2009 Statut Membre Dernière intervention 17 juin 2010 - 16 avril 2010 à 10:35
 fab1105 - 25 avril 2010 à 18:21
Bonjour, j'ai un petit soucis avec une requête qui ne fonctionne pas et j'aurai bien besoin d'un petit coup de main.
Voici le code qui ne marche pas :
$req = $bdd->prepare("INSERT INTO commentaires (pseudo,contenu,article_id) VALUES (:pseudo,:comment,:article_id)") or die(print_r($bdd->errorInfo()));
	$req->execute(array(
		'pseudo'=>$pseudo,
		'comment'=>$comment,
		'article_id'=>$p
	));
	$req->closeCursor();


et voici le code de la page entière si besoin, mais la seul requête qui ne marche pas est celle citée ci-dessus.
<?php
if(empty($_GET))
{
	header("Location: index.php");
}

if(!empty($_GET))
	{
		require_once("id_connection.php");
		extract($_GET);
	  $p = strip_tags($p);
	}

try
	{
		$bdd = new PDO('mysql:host='.$dbhost.';dbname='.$dbname,$dblogin,$dbpass);
    $bdd->exec('SET NAMES utf8');
	}
	
catch (Exeption $e)
	{
		die('Erreur :'.$e->getMessage());	
	}
	
	$req=$bdd->prepare("SELECT titre FROM articles WHERE id=:id");
	$req->execute(array(
		"id"=>$p
	));
	if($req->rowCount()==0)
	{
		header("Location: index.php");
	}
	$data = $req->fetch();
	$titre = $data['titre'];
	$req->closeCursor();
	
	if(!empty($_POST))
	{
		extract($_POST);
		$valid = true;
		
		if(empty($pseudo))
		{
			$valid = false;
			$erreurpseudo = "Pseudo requis";
		}
		
		if(empty($comment))
		{
			$valid = false;
			$erreurcomment = "Laissez vos commentaires";
		}
		
		
		if($valid)
		{
			$pseudo=strip_tags($pseudo);
			$comment=strip_tags($comment);
			
	
			
	//Ne fonctionne pas
	$req = $bdd->prepare("INSERT INTO commentaires (pseudo,contenu,article_id) VALUES (:pseudo,:comment,:article_id)") or die(print_r($bdd->errorInfo()));
	$req->execute(array(
		'pseudo'=>$pseudo,
		'comment'=>$comment,
		'article_id'=>$p
	));
	$req->closeCursor();
	
	unset($pseudo);
	unset($comment);
	}
	}
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $titre;?></title>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
</head>

<body>

<div id="content">

<?php

  
  $req=$bdd->prepare("SELECT * FROM articles WHERE id=:id");
  $req->execute(array(
                      "id"=>$p));
  $data = $req->fetch();
  
  echo "<h2>".$data['titre']."</h2>";
  echo "<div class=\"articles\">";
  echo "<p>".$data['contenu']."</p>";
  echo "<p class=\"date\">".date('j/n/Y G:i',strtotime($data['date']))."</p>";
  
  $req->closeCursor();
  
  

?>
<div class="form">
<form name="commenter" action="article.php?p=<?php echo $data['id']; ?>" method="post">
	<label for="pseudo">Pseudo :</label>
	<input type="text" name="pseudo" value="<?php if(isset($pseudo)) echo $pseudo; ?>" />
	<span class="error"><?php if(isset($erreurpseudo)) echo $erreurpseudo; ?></span>
		
	<label for="comment">Commentaires :</label>
	<textarea name="comment"><?php if(isset($comment)) echo $comment; ?></textarea>
	<span class="error"><?php if(isset($erreurcomment)) echo $erreurcomment; ?></span>
	
	<input type="submit" name="envoyer" value="Envoyer" />
</form>
</div>
<?php
$req=$bdd->prepare("SELECT * FROM commentaires WHERE article_id=:article_id");
  $req->execute(array(
                      "article_id"=>$p));
 while( $data = $req->fetch())
 {
	echo "<p>".$data['pseudo']."</p>";
	echo "<p>".$data['contenu']."</p>";
 }
?>
</div>
</div>
</body>
</html>

3 réponses

fab1105 Messages postés 114 Date d'inscription mardi 4 août 2009 Statut Membre Dernière intervention 17 juin 2010 7
16 avril 2010 à 11:04
Personne ?
0
Salut essaye sa :

$req = $bdd->prepare("INSERT INTO commentaires ('pseudo', 'contenu', 'article_id') VALUES (:pseudo,:comment,:article_id)") or die(print_r($bdd->errorInfo()));
$req->execute(array(
'pseudo'=>$pseudo,
'comment'=>$comment,
'article_id'=>$p
));
$req->closeCursor();

J'ai eu quasiment le même problème que toi. j'avais oublier les ' autour du nom des champs.
0
Salut DDS, merci pour ta réponse, mais en fait c'était juste ma table qui était mal faite au niveau de la clef étrangère. J'ai donc refais ma table et tout a fonctionné nickel.
Merci quand même.
0