CRUD Operation using PHP & Mongodb

2021.01.04 15:04

졸리운_곰 조회 수:26

 

CRUD Operation using PHP & Mongodb

 By Hardik Savani |  May 19, 2018 |  Category : PHP Bootstrap MongoDB
 
 
 
 
 
 

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

 

 

 

 

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'],

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

'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....

[출처] https://www.itsolutionstuff.com/post/crud-operation-using-php-mongodbexample.html

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
129 [Redis] php 세션 저장소를 redis 로 바꾸어 본 후기. file 졸리운_곰 2021.04.11 47
128 Creating a Website Design Templating System Using PHP 졸리운_곰 2021.02.13 22
127 Build a CRUD Operation using PHP & MongoBD 졸리운_곰 2021.01.04 23
» CRUD Operation using PHP & Mongodb file 졸리운_곰 2021.01.04 26
125 PHP and MongoDB Connection file 졸리운_곰 2021.01.04 63
124 PHP 기반의 Micro Frameworks 정리 졸리운_곰 2020.12.02 51
123 CKEditor 4 설치와 PHP 연동 하기 file 졸리운_곰 2020.11.22 47
122 [php] CKeditor 설정 및 적용 졸리운_곰 2020.11.22 38
121 [PHP]Fuelframework 설치 및 시작 방법(window10,xampp) file 졸리운_곰 2020.10.01 34
120 Building a Simple Blog App with MongoDB and PHP file 졸리운_곰 2020.09.13 50
119 웹 설문조사 시스템 & 설문조사를 잘 하는 방법 file 졸리운_곰 2020.09.10 185
118 ReactPHP Series 졸리운_곰 2020.07.01 50
117 Building a RESTful API Using ReactPHP and MySQL file 졸리운_곰 2020.07.01 44
116 [PHP 웹개발] MySQL 데이터베이스에서 mysqli(MySQL Improved) 사용법 졸리운_곰 2020.05.07 39
115 PHP 파일 업로드와 다운로드 만들기 file 졸리운_곰 2020.05.07 374
114 HOW TO INTEGRATE R WITH PHP : php와 R 언어의 연동 file 졸리운_곰 2020.05.05 250
113 XAMPP, PhpStorm, Hello World 출력하기 졸리운_곰 2020.03.27 40
112 Pico is a stupidly simple, blazing fast, flat file CMS. file 졸리운_곰 2020.03.19 30
111 directorylister php 사용법 file 졸리운_곰 2020.03.18 129
110 flat file 플랫파일시스템 : GRAV CMS file 졸리운_곰 2020.03.18 60
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED