Bonjour,
J'ai un problème avec mon code Php, j'ai un blog avec des articles, et je souhaite commenter les articles. je remplie le formulaire de commentaire ensuite j'envoie et e problème est qu'il n'y a rien d'afficher. Lorsque je vais dans la collection voici ce qu'il m'affiche
je ne sais pas pourquoi dans commentaire il y a null.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 { "_id": ObjectId("552028640faaf24450b7acda"), "title": "Hello World !", "content": "My new article is beautiful", "saved_at": ISODate("2015-04-04T18:07:32.519Z"), "comments": [ null,] }
Voici les fichiers php :
Comment.php :
Code php : 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 <?php $id = $_POST['article_id']; try { $mongodb = new MongoClient(); $collection = $mongodb->myblogsite->articles; } catch (MongoConnectionException $e) { die('Failed to connect to MongoDB ' . $e->getMessage()); } $article = $collection->findOne(array( '_id' => new MongoId($id) )); $comment = array( 'name' => $_POST['commenter_name'], 'email' => $_POST['commenter_email'], 'comment' => $_POST['comment'], 'posted_at' => new MongoDate() ); $collection->update(array( '_id' => new MongoId($id) ), array( '$push' => array( 'comments' => $comments ) )); header('Location: blog.php?id=' . $id); ?>
blog.php:
Code php : 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 <?php $id = $_GET['id']; try { $connection = new MongoClient(); $database = $connection->selectDB('myblogsite'); $collection = $database->selectCollection('articles'); } catch (MongoConnectionException $e) { die("Failed to connect to database " . $e->getMessage()); } $article = $collection->findOne(array( '_id' => new MongoId($id) )); ?> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="style1.css" /> <title>My Blog Site</title> </head> <body> <div id="contentarea"> <div id="innercontentarea"> <h1><?php echo $article['title']; ?></h1> <p> <?php echo $article[ 'content']; ?> </p> <div id="comment-section"> <h3>Comments</h3> <?php if (!empty($article[ 'comments'])): ?> <h3>comments</h3> <?php foreach($article[ 'comments'] as $comment): echo $comment[ 'name']. ' says...';?> <p> <?php echo $comment[ 'comment']; ?> </p> <span> <?php echo date('g:i a, F j', $comment['posted_at']); ?> </span> <br/> <br/> <br/> <?php endforeach; endif;?> <h3>Post your comment</h3> <form action="comment.php" method="post"> <span class="input-label">Name</span> <input type="text" name="commenter_name" class="comment-input" /> <br/> <br/> <span class="input-label">Email</span> <input type="text" name="commenter_email" class="comment-input" /> <br/> <br/> <textarea class="input-label" name="comment" rows="5"></textarea> <br/> <br/> <input type="hidden" name="article_id" value="<?php echo $article['_id']; ?>" /> <input type="submit" name="btn_submit" value="Save" /> </form> </div> </div> </div> </body> </html>
Merci de bien vouloir m'aider
Partager