Rachel Cole

Rachel Cole

1570347313

Build a CRUD Operation using PHP & MongoBD

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…

#php #mongodb #database #web-development

What is GEEK

Buddha Community

Build a CRUD Operation using PHP & MongoBD

Laravel 8 CRUD Operation Example

Hello Friend,

As you know Laravel 8 already officially released and today I will show you how to create CRUD operation in laravel 8, I have already perform many CRUD operations in my previous post like CRUD operation in ajax, CRUD operation in laravel 6 etc. So, today I will give you laravel 8 CRUD application example.

Laravel 8 CRUD Operation Example

https://websolutionstuff.com/post/laravel-8-crud-operation-example

#laravel #php #laravel 8 crud operation example #crud operation #laravel 8 crud tutorial #crud operation in laravel 8

Gordon  Murray

Gordon Murray

1623648178

How to Use Prepared Statement in PHP 8 For CRUD Application

Security is a measured challenge while creating an application in PHP. The raw SQL query in PHP can be injected through a malicious function. This may affect your entire functionality. Even you can lose your database or particular table and even any record. Due to the SQL injection, the vulnerable code or script can be injected into your SQL query. It is the most important thing to prevent SQL injection in your PHP application. I already shared a brief post on  SQL Injection and Prevention Techniques in PHP. The PHP frameworks like  Laravel,  CodeIgniter, etc are secured from the SQL injection. We can overcome the possibility of SQL injection using prepared statement. The PHP prepared statement query will generate a placeholder. It will prepare a SQL statement and later will bind the values to the placeholder. In this post, I will create a CRUD app using prepared statement.

Contents

  • 1 Prerequisites
  • 2 Create a Project Folder Structure
  • 3 Create Database and Table
  • 4 Database Connection For PHP Prepared Statement
  • 5 Create Master Layouts in PHP
  • 6 Create Index File For Displaying Data
  • 7 CRUD in PHP Using Prepared Statement
  • 8 Create Post
  • 9 Show Post
  • 10 Update Post
  • 11 Delete Post
  • 12 Conclusion
  • 12.1 Share this:
  • 12.2 Like this:
  • 12.3 Related

Prerequisites

For creating this CRUD app in php using prepared statement, you must have the below configuration.

  • Apache/Nginx Server
  • PHP >=7
  • MySQL >=5
  • VS Code (Optional)

Create a Project Folder Structure

Firstly, go to the htdocs directory if you are a windows user and installed xampp/wampp. If you are a Linux user then go to the var/www/html and create a new folder there. In my case, the folder name is crud. Inside this folder, I have created these two subfolders named assets and master.

Let me explain the file

#php #crud app #php 8 crud #php crud

Christa  Stehr

Christa Stehr

1595288376

How to use highchart in php with example

Highchart provides feature to draw different type of charts in our web application. Here, in this example i will let you know that how to use highchart in php application.

In this example, we will create chart using php and mysql. Actually highchart provides javascript library to create charts. We will only need to implement that library in our application.

We will need some data to generate chart, so here we will use mysql database and database query to fetch data from database.

So basucally here we will leran to implement simple dynamic column chart using highcharts library in php and ofcourse will use mysql database.

For for that, first thing we will need to create a database and tables where we will put some data. So for full example let’s follow the steps as given below.

Step 1: Create Database

Here for example, i will create a database named shopping and under this database we will need to create some tables, here i will create only two table one is to stroe customers information and another to store orders.

So run the following query in your query window.

Create customers table

CREATE TABLE IF NOT EXISTS `customers` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(30) NOT NULL,

  `email` varchar(30) NOT NULL,

  `phone` varchar(15) NOT NULL,

  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

Now you will to put some data into these two tables. You can put any data accoring to the column and your need. After putting data we can be able to show the chart according the the data.

Step 2: Create database configuration

Now we will need to create configuration file. So let’s create a new db configuration file **db_config.php **and put the following code into this file.

db_config.php

<?php
  $dbHost = "localhost";
  $dbDatabase = "shopping";
  $dbUser = "root";
        $dbPassword = ""; 

  $mysqli = mysqli_connect($dbHost, $dbUser, $dbPasswrod, $dbDatabase);
?>

#php #create highchart in php #generate highchart in php #high chart using php and mysql example #highchart example #how to implement high chart in php #how to use highchart

I am Developer

1597487472

Country State City Dropdown list in PHP MySQL PHP

Here, i will show you how to populate country state city in dropdown list in php mysql using ajax.

Country State City Dropdown List in PHP using Ajax

You can use the below given steps to retrieve and display country, state and city in dropdown list in PHP MySQL database using jQuery ajax onchange:

  • Step 1: Create Country State City Table
  • Step 2: Insert Data Into Country State City Table
  • Step 3: Create DB Connection PHP File
  • Step 4: Create Html Form For Display Country, State and City Dropdown
  • Step 5: Get States by Selected Country from MySQL Database in Dropdown List using PHP script
  • Step 6: Get Cities by Selected State from MySQL Database in DropDown List using PHP script

https://www.tutsmake.com/country-state-city-database-in-mysql-php-ajax/

#country state city drop down list in php mysql #country state city database in mysql php #country state city drop down list using ajax in php #country state city drop down list using ajax in php demo #country state city drop down list using ajax php example #country state city drop down list in php mysql ajax

Sigrid  Farrell

Sigrid Farrell

1624096385

Spring Boot CRUD Operations

In the video in this article, we take a closer look at the Spring Boot CRUD Operations Example alongside Exception Handling!

In the video below, we take a closer look at the Spring Boot CRUD Operations example with exception handling. Let’s get started!

#spring boot #spring boot tutorial for beginners #crud #crud #crud #spring boot crud operations