Tagged: CRUD Operation using PHP & Mongodb

Mongodb Insert Update Delete – CRUD Operation using PHP & Mongodb 0

Mongodb Insert Update Delete – CRUD Operation using PHP & Mongodb

Mongodb Insert Update Delete – CRUD Operation using PHP & Mongodb

Today, We want to share with you Mongodb Insert Update Delete – CRUD Operation using PHP & Mongodb.
In this post we will show you MongoDB: Basic Select, Insert, Update, Delete – CRUD, hear for MongoDB Inserting, Updating and Deleting using PHP we will give you demo and example for implement.
In this post, we will learn about how to retrieve data from mongodb using php with an example.

Phase 1: Make MongoDB database

//MongoDB CRUD Operations using PHP
mongo
> use pakadatabase
> db.articles.insert( { "name": "Angularjs", "information": "jaydeep" } )

Phase 2: Install package mongodb/mongodb Library

//CRUD Operation using PHP & Mongodb
composer require mongodb/mongodb

Phase 3: Make Config File for CRUD web App

Also mongodbexample database and collection simple name. our here database is mongodbexample “pakadatabase” and “articles” is our main collection.

<?php
require_once __DIR__ . "/vendor/autoload.php";
$collection = (new MongoDB\Client)->pakadatabase->articles;
?>

Phase 4: Make index Make edit delete files

At last Phase, I need to make Like main root files index.php, created.php, and edit.php last delete.php file. therefor let’s make files at source code bellow:

index.php

<?php
   session_start();
?>
<!DOCTYPE html>
<html>
<head>
   <title>PHP & MongoDB - CRUD Operation Tutorials - pakainfo.com</title>
   <link href="bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>PHP & MongoDB - CRUD Operation Tutorials - pakainfo.com</h1>

<a href="create.php" class="btn btn-success">Add Article</a>

<?php
   if(isset($_SESSION['success'])){
      echo "<div class='alert alert-success'>".$_SESSION['success']."</div>";
   }
?>
<table class="table table-borderd">
   <tr>
      <th>Article Name</th>
      <th>Information</th>
      <th>Action</th>
   </tr>
   <?php
      require 'db_conn.php';
      $articles = $collection->find([]);
      foreach($articles as $article) {
         echo "<tr>";
         echo "<td>".$article->name."</td>";
         echo "<td>".$article->information."</td>";
         echo "<td>";
         echo "<a href='edit.php?id=".$article->_id."' class='btn btn-success'>Edit</a>";
         echo "<a href='delete.php?id=".$article->_id."' class='btn btn-danger'>Delete</a>";
         echo "</td>";
         echo "</tr>";
      };


   ?>
</table>
</div>
</body>
</html>

create.php

below source code for create.php

<?php
session_start();
if(isset($_POST['submit'])){
   require 'db_conn.php';
   $insertOneResult = $collection->insertOne([
       'name' => $_POST['name'],
       'information' => $_POST['information'],
   ]);
   $_SESSION['success'] = "Article created successfully";
   header("Location: index.php");
}
?>

<!DOCTYPE html>
<html>
<head>
   <title>PHP & MongoDB - CRUD Operation Tutorials - pakainfo.com</title>
   <link href="bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container">
   <h1>Create Article</h1>
   <a href="index.php" class="btn btn-success">Back</a>
   <form method="POST">
      <div class="form-group">
         <strong>Name:</strong>
         <input type="text" name="name" required="" class="form-control" placeholder="Name">
      </div>
      <div class="form-group">
         <strong>Detail:</strong>
         <textarea class="form-control" name="information" placeholder="Detail" placeholder="Detail"></textarea>
      </div>
      <div class="form-group">
         <button type="submit" name="submit" class="btn btn-success">Submit</button>
      </div>
   </form>
</div>
</body>
</html>

edit.php

below source code for edit.php

<?php
session_start();

require 'db_conn.php';

if (isset($_GET['id'])) {
   $article = $collection->findOne(['_id' => new MongoDB\BSON\ObjectID($_GET['id'])]);
}
if(isset($_POST['submit'])){
   $collection->updateOne(
       ['_id' => new MongoDB\BSON\ObjectID($_GET['id'])],
       ['$set' => ['name' => $_POST['name'], 'information' => $_POST['information'],]]
   );
   $_SESSION['success'] = "Article updated successfully";
   header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
   <title>PHP & MongoDB - CRUD Operation Tutorials - pakainfo.com</title>
   <link href="bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>

<div class="container">
   <h1>Create Article</h1>
   <a href="index.php" class="btn btn-success">Back</a>
   <form method="POST">
      <div class="pakainfo form-group">
         <strong>Name:</strong>
         <input type="text" name="name" value="<?php echo $article->name; ?>" required="" class="form-control" placeholder="Name">
      </div>
      <div class="pakainfo form-group">
         <strong>Detail:</strong>
         <textarea class="form-control" name="information" placeholder="Detail" placeholder="Detail"><?php echo $article->information; ?></textarea>
      </div>
      <div class="pakainfo form-group">
         <button type="submit" name="submit" class="btn btn-success">Submit</button>
      </div>
   </form>
</div>
</body>
</html>

delete.php

<?php
//MongoDB: Basic Select, Insert, Update, Delete – CRUD
session_start();
require 'db_conn.php';
$collection->deleteOne(['_id' => new MongoDB\BSON\ObjectID($_GET['id'])]);
$_SESSION['success'] = "Article deleted successfully";
header("Location: index.php");
?>

We hope you get an idea about MongoDB: Basic Select, Insert, Update, Delete – CRUD
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

We hope This Post can help you…….Good Luck!.