Jump to content

Recommended Posts

Hello,

 

I don't really know where to post this, so I am posting this here. This script is in french so I will explain a bit. I want to make a quick blog with comments. I know nothing about PHP and I don't have time to learn because I have to do this to monday. In commentaires.php you can view the blog post you have clicked on with the comments related to it. When you post a comment the page commentaires_post.php inserts the author, comment text and date with time in the phpmyadmin table named "commentaires". I have a problem when I make a post, the id of the comment is not related to the blog post itself.

 

in the table "commentaires" in phpmyadmin:

id -> the id of the comment

id_billet means the id of the blog post (translation of "billet" is "note" or something like that)

auteur -> author

commentaire -> comment

date_commentaire -> date_comment

 

commentaires.php:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
    <head>
        <title>Mon blog</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
       
    <body>
        <h1>Mon super blog !</h1>
        <p><a href="index.php">Retour à la liste des billets</a></p>

<?php
// Connexion à la base de données
try
{
   $bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
}
catch(Exception $e)
{
        die('Erreur : '.$e->getMessage());
}

// Récupération du billet
$req = $bdd->prepare('SELECT id, titre, contenu, DATE_FORMAT(date_creation, \'%d/%m/%Y à %Hh%imin%ss\') AS date_creation_fr FROM billets WHERE id = ?');
$req->execute(array($_GET['billet']));
$donnees = $req->fetch();
?>

<div class="news">
    <h3>
        <?php echo htmlspecialchars($donnees['titre']); ?>
        <em>le <?php echo $donnees['date_creation_fr']; ?></em>
    </h3>
   
    <p>
    <?php
    echo nl2br(htmlspecialchars($donnees['contenu']));
    ?>
    </p>
</div>

<h2>Commentaires</h2>

<?php
$req->closeCursor(); // Important : on libère le curseur pour la prochaine requête

// Récupération des commentaires
$req = $bdd->prepare('SELECT auteur, commentaire, DATE_FORMAT(date_commentaire, \'%d/%m/%Y à %Hh%imin%ss\') AS date_commentaire_fr FROM commentaires WHERE id_billet = ? ORDER BY date_commentaire');
$req->execute(array($_GET['billet']));

while ($donnees = $req->fetch())
{
?>
<p><strong><?php echo htmlspecialchars($donnees['auteur']); ?></strong> le <?php echo $donnees['date_commentaire_fr']; ?></p>
<p><?php echo nl2br(htmlspecialchars($donnees['commentaire'])); ?></p>
<?php
} // Fin de la boucle des commentaires
$req->closeCursor();
?>








<!-- Ajouter un commentaire / Post a comment -->


   
    <form action="commentaires_post.php" method="post">
        <p>
        <label for="auteur">Auteur</label> : <input type="text" name="auteur" id="auteur" /><br />
        <label for="commentaire">Commentaire</label> :  <input type="text" name="commentaire" id="commentaire" /><br />
      <input type="hidden" name="id_billet" value="<?php echo $_GET['billet']; ?>" />
      
        <input type="submit" value="Envoyer" />
   </p>
    </form>
   </body>
</html>

 

commentaires_post.php:

 

<?php
// Connexion à la base de données
try
{
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}

// Insertion du commentaire à l'aide d'une requête préparée
$req = $bdd->prepare('INSERT INTO commentaires (id_billet, auteur, commentaire, date_commentaire) VALUES(?, ?, ?, NOW())');
$req->execute(array($_POST['id_billet'], $_POST['auteur'], $_POST['commentaire']));

// Redirection du visiteur vers la page de commentaires.php
header('Location: commentaires.php');
?>

Link to comment
https://forums.phpfreaks.com/topic/218962-blog-and-comment-problem/
Share on other sites

I suggest you go through this tutorial : http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/

 

It's extremely helpful, an easy step by step tutorial with screenshots too!

 

Good luck! Let us know how you get on!

 

- Greeky.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.