Build a CRUD Operation using PHP & MongoBD

Build a CRUD Operation using PHP & MongoBD

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.

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.

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
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 MongoDB\Client)->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 class="container">

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


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


<?php


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

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

   }


?>


<table class="table table-borderd">

   <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 class="container">

   <h1>Create Book</h1>

   <a href="index.php" class="btn btn-primary">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="detail" 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

<?php


session_start();


require 'config.php';


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

   $book = $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'], '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 class="container">

   <h1>Create Book</h1>

   <a href="index.php" class="btn btn-primary">Back</a>


   <form method="POST">

      <div class="form-group">

         <strong>Name:</strong>

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

      </div>

      <div class="form-group">

         <strong>Detail:</strong>

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

      </div>

      <div class="form-group">

         <button type="submit" name="submit" class="btn btn-success">Submit</button>

      </div>

   </form>

</div>


</body>

</html>

delete.php

<?php


session_start();

require 'config.php';


$collection->deleteOne(['_id' => new MongoDB\BSON\ObjectID($_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....

Advantages of Hiring PHP Developer for your Website Project

Advantages of Hiring PHP Developer for your Website Project

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is...

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is to learn the language yourself. That’s why PHP developers are there to make your life easy. This article will give us the advantages and requirements of Hire PHP Developer for our very own website project.

First of all, let us understand the value the right developer brings to the project and why it is important for your business.

A website is a major component of any company/business and is very important for its face value, the way it represents the company on the internet is critical for any business to succeed. This is the reason why companies are looking for PHP developers who can develop their webpage.

If you're planning to do an online business, your PHP programmer will be the first person to transfer your thinking onto the webpage. You should, therefore, employ developers from PHP to make your hypothetical idea a reality.

With this software programming language, PHP developers all-around can easily build website frameworks, web content management systems, web template systems, and various other web-based designs.

Some of the reasons why we need to outsource these developers are:

Not everyone is the best in each field, all of us have our specific skills and talents hence, PHP developers are also the best at what they do. The time and money spent on the training of the in house employees would be saved if the professional PHP developers are hired. Instead of multitasking, if the employees were to focus on what they’re good at it would increase productivity too.

The PHP developers would be much more professional than the in-house workers. It would lead to the seriousness of work. Hence, on-time delivery is guaranteed with hired PHP developers.

In addition to these benefits, you would also be able to track your project through every stage in constant communication with your online team. These advantages make it incredibly popular and smart to hire a PHP developer.

The PHP developers have in-depth knowledge of PHP, HTML and various frameworks in terms of technical capabilities. Hiring PHP developers are advised to give your website a professional look based on PHP.

Much of web success depends on the involvement of social media. The developer can add to your social networking pages a feature that explicitly redirects visitors. In addition, SEO experts also suggest better connections to the website's social network.

Just like a tailor stitches our dresses according to our preferences and is ready to make last-minute changes. A PHP developer will also be available at the nick of your call to make the website just the way you want it to be and have a customized solution for every problem.

Read also: Why & How to Hire Dedicated PHP Developer

At some point in your business, you’re going to have problems regarding your webpage due to the rapidly changing technology, instead of struggling with ideas like these and not being able to come up with an appropriate solution a PHP web developer could help us with our problems just like any technician would help us with the problems we face in our offices or any architect would help us with designing the structure of a building or any interior designer would help us with setting up our home. The PHP development company are hubs of workers who would help us overcome these problems and are always there.

Source by

Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]

Web Development Services

Web Development Services

As one of the best Web Application Development Company, it provides a fine quality mobile app development service at an affordable price. Especially, it encourage start-ups that have unique ideas, by offering a more competitive price

HireFullStackDeveloperIndia is rated as one of the top Web Application Development Company in India by various industry magazines and review sites. They have a right blend of award-winning designers, expert programmers and Google certified digital marketers which make them a unique one-stop solution for hundreds of our clients, spread across all countries.

A Good website reflects not only your business but also it is one of the main factors why a potential customer would convert into Client. A good website design helps increase traffic driving leads to grow business. The best web design company create a custom design for each corporate website so as to help them meet their business goals.

Get Quote: https://hirefullstackdeveloperindia.com/get-a-quote/