CRUD Operation using PHP & Mongodb

CRUD Operation using PHP & Mongodb

In this post we will give you information about CRUD Operation using PHP & Mongodb. Hear we will give you detail about CRUD Operation using PHP & MongodbAnd how to use it also give you demo for it if it is necessary.

Hi Developer.

in this tutorial. i would like to share with you full source code of insert update delete and view application using PHP Mongodb. we will use mongodb as database. we will create step by step add update delete module using PHP MongoDB. Using this example you can easily use mongodb query like find, select, insert, update, delete, search etc in php. also you can learn how to make connection between php and mongodb.

As we know mongodb is a very popular open source, document based NoSQL database. if you have large number of data or if it can become more data on database then you should use Mongodb as database. Mongodb is a document based NoSQL database that way it store data in less memory and you can fetch quick records.

Few days ago i posted tutorials about crud app with mongodb using Laravel 5.6, you can also read from here: Laravel 5.6 CRUD Operation using MongoDB.

So, you need to just follow bellow step and will get full example of crud application. you can also download full script in free. But make sure you need to install mongodb and composer in your system.

Preview:

Step 1: Create MongoDB database

First thing is, we require to create mongodb database and books collection. So successful MongoDB installation open the terminal, connect to MongoDB, create a database and collection and insert a books like as bellow command.

mongo

> use hddatabase

> db.books.insert( { "name": "laravel", "detail": "test" } )

Step 2: Install mongodb/mongodb Library

In this step we need to install mongodb/mongodb library via the Composer package manager. so first we will create root folder for php and then run bellow command in your terminal:

composer require mongodb/mongodb

Also see:Codeigniter 3 – CRUD(Create, Read, Update and Delete) using JQuery Ajax, Bootstrap, Models and MySQL

Step 3: Create Config File for CRUD App

here we will create configuration file for crud app. in this file we will write code for connection with mongodb. so let’s create file and put bellow code. Make sure you need to set port, url, username and password.

Also database and collection name. our database is “hddatabase” and “books” is our collection.

config.php

<?php


require_once __DIR__ . "/vendor/autoload.php";


$collection = (new MongoDBClient)->hddatabase->books;


?>

Step 4: Create index create edit delete files

At last step, we need to create index.php, created.php, edit.php and delete.php file. so let’s create files at bellow:

index.php

<?php

session_start();

?>

<!DOCTYPE html>

<html>

<head>

<title>PHP & MongoDB - CRUD Operation Tutorials - ItSolutionStuff.com</title>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">

</head>

<body>


<div >

<h1>PHP & MongoDB - CRUD Operation Tutorials - ItSolutionStuff.com</h1>


<a href="create.php" >Add Book</a>


<?php


if(isset($_SESSION['success'])){

echo "<div class='alert alert-success'>".$_SESSION['success']."</div>";

}


?>


<table >

<tr>

<th>Name</th>

<th>Details</th>

<th>Action</th>

</tr>

<?php


require 'config.php';


$books = $collection->find([]);


foreach($books as $book) {

echo "<tr>";

echo "<td>".$book->name."</td>";

echo "<td>".$book->detail."</td>";

echo "<td>";

echo "<a href='edit.php?id=".$book->_id."' class='btn btn-primary'>Edit</a>";

echo "<a href='delete.php?id=".$book->_id."' class='btn btn-danger'>Delete</a>";

echo "</td>";

echo "</tr>";

};


?>

</table>

</div>


</body>

</html>

create.php

<?php


session_start();


if(isset($_POST['submit'])){


require 'config.php';


$insertOneResult = $collection->insertOne([

'name' => $_POST['name'],

'detail' => $_POST['detail'],

]);


$_SESSION['success'] = "Book created successfully";

header("Location: index.php");

}


?>


<!DOCTYPE html>

<html>

<head>

<title>PHP & MongoDB - CRUD Operation Tutorials - ItSolutionStuff.com</title>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">

</head>

<body>


<div >

<h1>Create Book</h1>

<a href="index.php" >Back</a>


<form method="POST">

<div >

<strong>Name:</strong>

<input type="text" name="name" required="" placeholder="Name">

</div>

<div >

<strong>Detail:</strong>

<textarea name="detail" placeholder="Detail" placeholder="Detail"></textarea>

</div>

<div >

<button type="submit" name="submit" >Submit</button>

</div>

</form>

</div>


</body>

</html>

edit.php

<?php


session_start();


require 'config.php';


if (isset($_GET['id'])) {

$book = $collection->findOne(['_id' => new MongoDBBSONObjectID($_GET['id'])]);

}


if(isset($_POST['submit'])){


$collection->updateOne(

['_id' => new MongoDBBSONObjectID($_GET['id'])],

['$set' => ['name' => $_POST['name'], 'detail' => $_POST['detail'],]]

);


$_SESSION['success'] = "Book updated successfully";

header("Location: index.php");

}


?>


<!DOCTYPE html>

<html>

<head>

<title>PHP & MongoDB - CRUD Operation Tutorials - ItSolutionStuff.com</title>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">

</head>

<body>


<div >

<h1>Create Book</h1>

<a href="index.php" >Back</a>


<form method="POST">

<div >

<strong>Name:</strong>

<input type="text" name="name" value="<?php echo $book->name; ?>" required="" placeholder="Name">

</div>

<div >

<strong>Detail:</strong>

<textarea name="detail" placeholder="Detail" placeholder="Detail"><?php echo $book->detail; ?></textarea>

</div>

<div >

<button type="submit" name="submit" >Submit</button>

</div>

</form>

</div>


</body>

</html>

delete.php

Also see:Laravel – MongoDB CRUD Tutorial

<?php


session_start();

require 'config.php';


$collection->deleteOne(['_id' => new MongoDBBSONObjectID($_GET['id'])]);


$_SESSION['success'] = "Book deleted successfully";

header("Location: index.php");


?>

Now, you can run at your local.

You can also download from bellow link.

I hope it can help you….

Hope this code and post will helped you for implement CRUD Operation using PHP & Mongodb. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

5  +  5  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US