forked from adavijit/AVDOJO-TUTORIALS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpMongo.php
48 lines (43 loc) · 1.06 KB
/
phpMongo.php
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
<?php
require 'vendor/autoload.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
<form method="post" action="">
<input name="text" type="text" placeholder="enter some text" />
<input type="submit" value="Save" name="submit">
</form>
</div>
<br/>
<div>
<form method="post" action="">
<input name="show" type="submit" value="Show">
</form>
</div>
</body>
</html>
<?php
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->mongophp->details;
if(isset($_POST['submit'])){
$text = $_POST['text'];
$result = $collection->insertOne(['newText'=> $text]);
echo "inserted with object id ".$result->getInsertedId();
}
if($_POST['show'])
{
$result = $collection->find()->toArray();
foreach($result as $temp)
{
echo $temp['newText']."<br/>";
}
}
?>