3 pièce(s) jointe(s)
Fatal error: Uncaught Error: Call to a member function prepare() on null..
Bonjour,
J'ai un souci avec mon code et je n'arrive pas à voir où je me suis plantée.. ça fait plus d'une heure que j'ai la tête dedans, je crois que je ne vois plus rien..
Voici le message d'erreur : Pièce jointe 505050
Le code concerné côté view :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| <article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<h3><?= htmlspecialchars($post['title']) ?></h3>
<p>
<div id="testarticle">
<p><?= nl2br(htmlspecialchars($post['content'])) ?></p></div>
<br />
<h2>Commentaires</h2>
<form action="XXX" method="post">
<div>
<label for="name">Auteur</label>
<br />
<input type="text" id="name" name="name" />
</div>
<div>
<label for="content">Commentaire</label>
<br />
<textarea id="content" name="content"></textarea>
</div>
<div>
<input type="submit" />
</div>
</form>
</p>
</div>
</div>
</div>
</article>
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<?php
while ($comment = $comments->fetch())
{
?>
<h5><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date'] ?></h5>
<p>
<?= nl2br(htmlspecialchars($comment['comment'])) ?>
<br />
</p>
<div>
<a href="ajouter php pour signaler"><img src="/img/signaler.png" class="signaler" alt="signaler"/>Signaler</a>
</div>
<?php
}
?>
<br />
</div>
</div>
</div>
</article> |
Côté model :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <?php
function getPosts()
{
$db = dbConnect();
$req = $db->query('SELECT id, title, content, DATE_FORMAT(date, \'%d/%m/%Y\') AS date FROM article ORDER BY date DESC LIMIT 0, 5');
return $req;
}
function getPost($postId)
{
$db = dbConnect();
$req = $db->prepare('SELECT id, title, content, DATE_FORMAT(date, \'%d/%m/%Y\') AS date FROM article WHERE id = ?');
$req->execute(array($postId));
$post = $posts->fetch();
return $post;
}
function getComments($postId)
{
$db = dbConnect();
$comments = $db->prepare('SELECT id, author, content, DATE_FORMAT(comment_date, \'%d/%m/%Y\') AS comment_date FROM comment WHERE FK_post = ? ORDER BY comment_date DESC');
$comments->execute(array($postId));
return $comments;
}
// Connexion à la BDD
function dbConnect()
{
$servername = "localhost";
$username = "XusernameX";
$password = "XmdpX";
$database = "XdbnomX";
try
{
$db = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
} |
Côté controller :
Code:
1 2 3 4 5 6 7 8 9 10 11
| <?php
require('../modeles/articlemodel.php');
if (isset($_GET['id']) && $_GET['id'] > 0) {
$post = getPost($_GET['id']);
$comments = getComments($_GET['id']);
require('../vues/articleview.php');
}
else {
echo 'Erreur : aucun identifiant de billet envoyé';
} |
Du côté de ma BDD, j'ai dans ma table article :
Pièce jointe 505059
Et côté commentaire :
Pièce jointe 505062
Si quelqu'un peut prendre le temps de m'aider, ce serait top :D