Insert Data in MongoDb using PHP
In this tutorial, We will learn How to insert Data in MongoDb using PHP
Step 1: Download MongoDB from here https://www.mongodb.com/try/download/community and Install it
Step 2: Download the MongoDB Driver DLL
Go to https://pecl.php.net/package/mongodb
Click on the “DLL” link for Windows downloads.

Step 3: Choose the correct version matching your PHP:
PHP 8.2 → select php_mongodb–8.2-ts-x64.dll (Thread Safe if using Apache/XAMPP)
Match your architecture: x64 if 64-bit PHP.
Download the .dll file.

Download the .dll file.
Step 4: Place the DLL in the PHP Ext Folder
Copy the downloaded php_mongodb.dll to your PHP ext directory:

Paste in the \xampp\php\ext
Example:
C:\xampp\php\ext
Step 5: Enable the Extension in php.ini
Open your php.ini (e.g., C:\xampp\php\php.ini)
Add the following line at the end:
extension=mongodb
Step 6: Create an HTML form
<form method="post">
<div class="input-box">
<input type="text" name="name" placeholder="Enter your name" required>
</div>
<div class="input-box">
<input type="text" name="email" placeholder="Enter your email" required>
</div>
<div class="input-box">
<input type="text" name="mobile" placeholder="Enter mobile number" required>
</div>
<div class="input-box">
<input type="text" name="city" placeholder="Enter city name" required>
</div>
<div class="input-box">
<input type="text" name="state" placeholder="Enter state name" required>
</div>
<div class="input-box button">
<input type="submit" value="Submit Now">
</div>
</form>
Step 7: Create a Database Connection File (db.php)
<?php
try {
// Create a connection to MongoDB
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
} catch (MongoDB\Driver\Exception\Exception $e) {
// Handle connection errors
die("MongoDB Connection Failed: " . $e->getMessage());
}
Step 6: PHP code to handle the form data
<?php
// Include the MongoDB connection
require 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Collect sanitized form data
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$mobile = filter_input(INPUT_POST, 'mobile', FILTER_SANITIZE_STRING);
$city = filter_input(INPUT_POST, 'city', FILTER_SANITIZE_STRING);
$state = filter_input(INPUT_POST, 'state', FILTER_SANITIZE_STRING);
try {
// Prepare the document
$bulk = new MongoDB\Driver\BulkWrite;
$doc = [
'_id' => new MongoDB\BSON\ObjectID(),
'name' => $name,
'email' => $email,
'mobile' => $mobile,
'city' => $city,
'state' => $state,
'created_at' => new MongoDB\BSON\UTCDateTime()
];
$bulk->insert($doc);
// Execute insertion into "mydatabase.users"
$manager->executeBulkWrite('mydatabase.users', $bulk);
$message = "<p style='color:green;'>Data submitted successfully!</p>";
} catch (MongoDB\Driver\Exception\Exception $e) {
echo "Error inserting data: " . $e->getMessage();
}
}
?>
Explanation of the Above Code
require 'db.php'; Database Connection file
Collect the form data
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$mobile = filter_input(INPUT_POST, 'mobile', FILTER_SANITIZE_STRING);
$city = filter_input(INPUT_POST, 'city', FILTER_SANITIZE_STRING);
$state = filter_input(INPUT_POST, 'state', FILTER_SANITIZE_STRING);
Prepare the Data for Insertion
$bulk = new MongoDB\Driver\BulkWrite;
$doc = [
'_id' => new MongoDB\BSON\ObjectID(),
'name' => $name,
'email' => $email,
'mobile' => $mobile,
'city' => $city,
'state' => $state,
'created_at' => new MongoDB\BSON\UTCDateTime()
];
$bulk->insert($doc);
BulkWrite: used to insert one or more documents at once.
_id: is a unique identifier. MongoDB uses it to uniquely identify each document.
new MongoDB\BSON\UTCDateTime(): Store the current Timestamp in MongoDB format.
$manager->executeBulkWrite('mydatabase.users', $bulk);
mydatabase.users → mydatabase is the database name, users is the name of the collection.
Executes all queued operations in $bulk (in this case, a single insert).
MongoDB will automatically create the database and collection if they don’t exist.
