I want to delete a document with PHP. My database is Mongodb.
This is my database article : The collection is articles
{ "_id" : ObjectId("55be575271dd7ec905b7acda"), "title" : "test", "content" : "Test", "user" : "Paul", "saved_at" : ISODate("2015-08-02T17:49:45.776Z"), }
>
So for example if I want to delete an article. I have tried this code :
ListArticle.php :
try {
$connection = new MongoClient();
$database = $connection->selectDB('blog');
$collection = $database->selectCollection('articles');
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$query=array();
$cursor=$collection->find($query);
foreach ($cursor as $doc) {
echo " Title : ".$doc['title']." "."by ".$doc['user'];
?>
<form method="POST" action="script.php" onsubmit="return confirm('do you want to delete this article ?')">
<br/>
<?php
echo "<input type='submit' value='Delete'>";
$v=$doc["content"];
echo "<input type='hidden' name='content' value=$v>";
echo "</form><br/>";
}
script.php :
try
{
$connection = new MongoClient();
$database = $connection->selectDB('blog');
}
Catch(MongoException $e)
{
die("Failed to connect to database " . $e->getMessage());
}
$collection = $database->articles;
$query=array("content"=>$_POST["content"]);
$collection->remove($query);
echo "<p>Success !</p>";
?>
When I click delete article it does not remove
Thanks with the help