我已经创建了简单的应用程序php表单以在mongodb集合中插入数据,驱动程序已连接,但数据未插入到集合中 一切都安装在Windows上
我正在使用Mongodb 4.0.5 php 7.2.10
xampp 3.2.2
这是要填写的html表单
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<form id="search-form" method="post" action="registerdc.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Diagnostic Center Name</label>
<input type="text" name="DCName" class="form-control">
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="form_Add1">Address 1</label>
<textarea id="form_Add1" name="Add1" class="form-
control" ></textarea>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="form_Add2">Address 2</label>
<textarea id="form_Add2" name="Add2" class="form-
control" ></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label for="form_Mobile">Mobile No</label>
<input id="form_mobile" type="text" name="mobile" class="form-control" >
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="form_mailID">Email ID</label>
<input id="form_mailID" type="text" name="mailID"
class="form-control" >
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="form_State">State</label>
<select class="form-control" id="Select3"
name="Select3">
<option>Maharashtra</option>
<option>Rajasthan</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label for="form_Pincode">Pin Code</label>
<input id="form_Pincode" type="text" name="pincode"
class="form-control" >
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" class="submit" value="Add">
</div>
</div>
</div>
</div>
</form>
</body>
</html>
此php表单将html表单中的数据插入到mongodb数据库retest&collection test2中
<?php
//including the database connection file
include_once("config2.php");
if(isset($_POST['Submit'])) {
$doc = array (
'DCname' => $_POST['DCname'],
);
// checking empty fields
$errorMessage = '';
foreach ($doc as $key => $value) {
if (empty($value)) {
$errorMessage .= $key . ' field is empty<br />';
}
}
if ($errorMessage) {
// print error message & link to the previous page
echo '<span style="color:red">'.$errorMessage.'</span>';
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
$bw->insert($doc);
$mng->executeBulkWrite('retest.test2', $bw);
//display success message
echo "<font color='green'>Data added successfully.";
}
}
?>
mongodb数据的配置如下
<?php
require 'vendor/autoload.php';
$mng = new MongoDB\Driver\Manager();
echo " Driver connected";
$bw = new MongoDB\Driver\Bulkwrite;
?>
The code runs showing message "Driver Connected" but does not inserts
data in
mongodb collection.