Bonjour à tous,

Je travaille actuellement sur la mise en place d'un blog avec espace commentaires selon l'article sélectionné selon l'architecture MVC en PHP et je rencontre un petit problème lorsque je souhaite intégrer la fonctionnalité de modification des commentaires.

Je vous expose d'abord mon code :

model.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
59
60
61
62
63
64
65
66
67
68
<?php
 
function getPosts()
{
	$db = dbConnect();
 
	$request = 'SELECT id, title, content, DAY(post_date) AS day, MONTH(post_date) AS month, YEAR(post_date) AS year FROM posts ORDER BY post_date DESC';
	$response = $db->query($request);
 
	return $response;
}
 
function getPost($idPost)
{
	$db = dbConnect();
 
	$requestPost = 'SELECT id, title, content, DAY(post_date) AS day, MONTH(post_date) AS month, YEAR(post_date) AS year FROM posts WHERE id = ?';
	$responsePost = $db->prepare($requestPost);
	$responsePost ->execute(array($idPost));
	$post = $responsePost->fetch();
 
	return $post;
}
 
function getComments($idPost)
{
	$db = dbConnect();
 
	$requestComments = 'SELECT id, id_post, author, comment, DAY(comment_date) AS day, MONTH(comment_date) AS month, YEAR(comment_date) AS year FROM comments WHERE id_post = ? ORDER BY comment_date DESC LIMIT 0, 5';
	$responseComments = $db->prepare($requestComments);
	$responseComments ->execute(array($idPost));
 
	return $responseComments;
}
 
function postComment($idPost, $author, $comment)
{
	$db = dbConnect();
 
	$requestPostComment = 'INSERT INTO comments(id_post, author, comment) VALUES(?, ?, ?)';
	$responsePostComment = $db->prepare($requestPostComment);
	$responsePostComment ->execute(array($idPost, $author, $comment));
	return $responsePostComment;
}
 
function eComment($comment, $idComment)
{
	$db = dbConnect();
 
	$requestEditComment = 'UPDATE comments SET content = ? WHERE id = ?';
	$responseEditComment = $db->prepare($requestEditComment);
	$responseEditComment ->execute(array($comment, $idComment));
	return $responseEditComment;
}
 
function dbConnect()
{
		try
	{
		$db = new PDO('mysql:host=localhost;dbname=blog;charset=utf8', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
	}
		catch(Exception $e)
	{
		die('Erreur : '.$e->getMessage());
	}
 
	return $db;
}
controller.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
<?php
require('model.php');
 
function listPosts()
{
	$response = getPosts();
	require('indexView.php');
}
 
function post()
{
	$post = getPost($_GET['id']);
    $responseComments = getComments($_GET['id']);
    require('postView.php');
}
 
function addComment($idPost, $author, $comment)
{
    $responsePostComment = postComment($idPost, $author, $comment);
 
    if ($responsePostComment === false) {
        die('Impossible d\'ajouter le commentaire !');
    }
    else {
        header('Location: index.php?action=post&id=' . $idPost);
    }
}
 
function editComment($comment, $idComment)
{
	$responseComments = getComments($_GET['id']);
	$post = getPost($_GET['id']);
 
    $responseEditComment = eComment($comment, $idComment);
 
    if ($responseEditComment === false) {
        die('Impossible de modifier le commentaire !');
    }
    else {
        header('Location: index.php?action=post&id=' . $idPost);
    }
    require('editComment.php');
}
indexView.php (vue de la liste des billets) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php $title = 'Mon Blog'; ?>
<?php ob_start(); ?>
	<h1>Blog</h1>
	<h2>Billets du blog :</h2>
 
	<?php
	while ($posts = $response->fetch())
	{
	?>
		<div class="news">
			<h3>
				<?= htmlspecialchars($posts['title'])?> publié le 
				<?= $posts['day']?>/<?= $posts['month']?> - <?= $posts['year']?>
			</h3>
			<p><?= htmlspecialchars($posts['content']); ?><br><a href="post.php?id=<?= $posts['id']?>;">Commentaires</a></p>
		</div>
	<?php
	}
	$response->closeCursor();
	?>
<?php $content = ob_get_clean(); ?>
 
<?php require('template.php'); ?>
postView.php (vue du billet sélectionné) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
<?php $title = 'Mon Blog'; ?>
<?php ob_start(); ?>
    <h1>Billet sélectionné</h1>
    <p><a href="index.php">Retour à la liste des billets</a></p>
 
    <div class="news">
        <h3>
            <?= $post['title']?> publié le 
            <?= $post['day']?>/<?= $post['month']?> - <?= $post['year']?>
        </h3>
        <p><?= $post['content']?></p>
    </div>
 
    <h2>Commentaires</h2>
 
    <?php
    while ($comment = $responseComments->fetch())
    {
    ?>
        <div class="news">Commentaire de 
            <?= htmlspecialchars($comment['author'])?> publié le 
            <?= $comment['day']?>/<?= $comment['month']?> - <?= $comment['year']?></br>
            <?= htmlspecialchars($comment['comment'])?><a href="editComment.php?action=editComment&amp;id=<?= $comment['id'] ?>">Modifier</a>
        </div>
    <?php
    }
    ?>
 
    <form action="index.php?action=addComment&amp;id=<?= $post['id'] ?>" method="post">
        <div>
            <label for="author">Auteur</label><br />
            <input type="text" id="author" name="author" />
        </div>
        <div>
            <label for="comment">Commentaire</label><br />
            <textarea id="comment" name="comment"></textarea>
        </div>
        <div>
            <input type="submit" />
        </div>
    </form>
<?php $content = ob_get_clean(); ?>
 
<?php require('template.php'); ?>
index.php (routeur) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
59
60
61
62
<?php
 
require ('controller.php');
 
if (isset($_GET['action'])) {
    if ($_GET['action'] == 'listPosts')
    {
        listPosts();
    }
    elseif ($_GET['action'] == 'post')
    {
        if (isset($_GET['id']) && $_GET['id'] > 0)
        {
            post();
        }
        else 
        {
            echo 'Erreur : aucun identifiant de billet envoyé';
        }
    }
    elseif ($_GET['action'] == 'addComment') 
    {
        if (isset($_GET['id']) && $_GET['id'] > 0) 
        {
            if (!empty($_POST['author']) && !empty($_POST['comment'])) 
            {
                addComment($_GET['id'], $_POST['author'], $_POST['comment']);
            }
            else 
            {
                echo 'Erreur : tous les champs ne sont pas remplis !';
            }
        }
        else 
        {
            echo 'Erreur : aucun identifiant de billet envoyé';
        }
    }
 
    elseif ($_GET['action'] == 'editComment') 
    {
        if (isset($_GET['id']) && $_GET['id'] > 0) 
        {
            if (!empty($_POST['newComment'])) 
            {
                editComment($_POST['newComment'], $_GET['id']);
            }
            else 
            {
                echo 'Erreur : tous les champs ne sont pas remplis !';
            }
        }
        else 
        {
            echo 'Erreur : aucun identifiant de billet envoyé';
        }
    }
 
}
else {
    listPosts();
}
editComment.php (le formulaire permettant d'éditer un commentaire) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php $title = 'Mon Blog'; ?>
<?php ob_start(); ?>
	<h1>Commentaire</h1>
	<h2>Modifier le commentaire :</h2>
 
	<?php $comment = $responseComments->fetch();?>
	<?php echo $comment['author'] . ' :<br>'?>
 
	<form action="index.php?action=addComment&amp;id=<?= $post['id'] ?>" method="post">
        <div>
            <label for="newComment">Commentaire</label><br />
            <input type="text" name="newComment"/>
        </div>
        <div>
            <input type="submit" />
        </div>
    </form>
 
<?php $content = ob_get_clean(); ?>
 
<?php require('template.php'); ?>
Le problème se situe dans le editComment.php : lorsque je clique sur le lien "modifier" qui redirige vers la page editComment, on m'indique cette erreur :

( ! )Notice: Undefined variable: responseComments in C:\wamp64\www\blog\editComment.php on line6
Call Stack
# Time Memory Function Location
1 0.0030 405000 {main}( ) ...\editComment.php:0
( ! ) Fatal error: Uncaught Error: Call to a member function fetch() on null in C:\wamp64\www\blog\editComment.php on line 6
( ! ) Error: Call to a member function fetch() on null in C:\wamp64\www\blog\editComment.php on line 6
Call Stack
# Time Memory Function Location
1 0.0030 405000 {main}( ) ...\editComment.php:0
Pourtant, je pense avoir correctement déclaré cette variable dans la fonction editComment au sein de mon controller.php.

De ce fait, j'ignore d'où peut provenir mon erreur et compte sur votre aide afin de bien vouloir m'éclairer s'il vous plait