1

I'm doing a little blog in PHP. The base of my blog works, but I wanted to improve it by putting a form to add comments and it sends me 2 errors and there I dry, I can not understand why this form (which works very well in the exercise of the minichat) makes to plant my page of the comments. These are the mistakes I get back:

Undefined index: billet in C: \ wamp64 \ www \ BLOG \ comments.php on line 24;
Undefined index: billet in C: \ wamp64 \ www \ BLOG \ comments.php on line 48

I understand that there is a problem in lines 24 and 48 but I do not understand why when I remove the form these 2 lines are no longer a problem, just when it became funny. Help please!

I put you my code.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Mon blog</title>
        <link rel="stylesheet" href="CSS/style.css" />
    </head>

    <body>
       <h1>Mon Premier 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;charset=utf8', '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><i>le <?php echo $donnees['date_creation_fr']; ?></i></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();
        ?>

        <div>
           <form action="commentaires_post.php" method="post">
               <p>
                    <label for="auteur"><u><b>Auteur / Pseudo</b></u></label> : 
                    <input type="text" name="auteur" id="auteur" style="border-radius:20px;background-color:lightgrey;" /><br /><br />
                    <label for="commentaire"><u><b>Commentaire</b></u></label> : 
                    <input type="text" name="commentaire" id="commentaire" style="width:150px; height:100px;border-radius:20px;background-color:lightgrey;"/><br />
                    <input type="submit" value="Envoyer" style="border-radius:20px;background-color:#000;color:goldenrod;" />
                </p>
            </form>
        </div>
    </body>
</html>
John Conde
  • 217,595
  • 99
  • 455
  • 496

0 Answers0