CRUD Operations Using PHP and MongoDB

This Tutorial help to Create CRUD(Create, Read, Update and Delete) Operations using Mongo database and PHP. I have already shared tutorial to Create Bootstrap Table Listing Using Mongo. I am using php7 with mongodb.

I am extending above tutorial and adding add, edit and delete functionality.I will restructure code for this tutorial. The Mongo connection object will go into separate file.I am assuming you have installed mongodb into your machine, if not Please read this tuorial. We also need mongo libs driver for php, that help to php for create connection with mongo and operation.

PHP CRUD Operation Using PHP

Let us start how to perform CRUD (Create/Read/Update/Delete) operations in MongoDB. We will create following files into this project.

  • index.php: This file will responsible for main application entry file and listing of data.
  • connection.php: This file will have mongo db connection information.
  • add.php: This is view file for add a record.
  • edit.php: This is view file for edit a record.

How to Create Connection Mongo DB with PHP

We will create connection.php file and added below code into this file.

We have passed dbname and mongo server hostname.

HTML Table Listing with Actions Button

I will modify index.php and added action button for add/edit and delete record.

we will include connection.php file into the top of index.php file –

We have also fetch all data from mongo database using cursor query and will use into table to display.

Let’s Create HTML action buttons into index.php file to add, edit and delete record –

NameAgeRoll NumberAction
Edit

How To Add Record Using MongoDb and PHP

We will add a new record using php. The record will insert into mongo database.Let’s create add.php file –

“, $message[‘errors’]); ?>

Add Employee

In the above code, first we will display errors and success message.We have added add a record html view using bootstrap css classes.

The Above HTML form will post data to server using HTTP Post method, We will get posted data into same add.php page.The following code will add into top of the add.php file.

First line will have the mongo db connection object and then check method is POST or GET, if the method is GET then we will simple show the add a record view, When the form post the data then Method would be POST and validate posted data.

We are displaying message if the validation has been failed, Otherwise inserting a new record into the mongo database.The mongo insert() method help to add a new record into mongodb collection.

How To Edit A Record Using MongoDb and PHP

Let’s a edit a existing record using PHP into Mongo database.Create a edit.php file and added below HTML code.This will display edit form to end user to enable edit record functionality.

“, $message[‘errors’]); ?>

Edit Employee

We have added div to display error and success message notification.The next div have contains edit form, we are using bootstrap css framework to display view file.We re suing HTTP POST method to post form data to the server.

The following code will add into top of the edit.php file.

The first, We have added mongo connection file into here to access mongo object.The HTTP method help to identify the form data has been post or not – If the form data is not posted then we will show edit form layout, if the form data posted then we will process data validation. After successfully validating data, we are updating data into the mongo db.

We are displaying message if the validation has been failed.We are inserting a new record into the mongo database when the validation passed.The mongo update() method help to update existing record into the mongodb.

How To Delete Record into MongoDb Using PHP

I have already added delete action button, Now we will add functionality to delete record from mongo database.Let’s add below code into the top of the index.php file –

Conclusion:

Created mongo db connection with PHP. We have also created Add, Edit and Delete CRUD operation using mongddb and PHP. The mongo libs has many inbuilt method to support daily uses mongodb operation.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.