Reputation: 11
after connecting with Mongodb i have created database project =>use project then i have created collection =>db.createCollection("WaterMoneteringSystem",{capped:true,size:2147483648,max:20000})
then i want to enter the values into the collection via php so this is the code i have written
<?php
$d=$_POST['dte'];
$t=$_POST['tme'];
$PH=$_POST['ph'];
$TU=$_POST['tbdity'];
$RO=$_POST['RdcdOxdtn'];
if($PH>14 || $PH<0)
{
echo "error";
}
else
{
$m = new MongoClient();
echo "Connection to database successfully";
select a database
$db = $m->project;
echo "Database project selected";
$collection = $db->WaterMoneteringSystem;
echo "Collection selected succsessfully";
$document = array(
"Date" => "$d",
"Time" => "$t",
"PH" => "$PH",
"Turbidity" => "$TU",
"Reduced Oxidation" =>"$RO"
);
$collection->insert($document);
echo "Document inserted successfully";
}
?>
But this is not working
thank you
Upvotes: 1
Views: 1618
Reputation: 3200
Okay, I recreated what you had and it seemed to work for me. Here is the exact code that I used:
$mongo = new MongoClient(); $db = $mongo->project;
$d = '2014-04-14';
$t = '18:14:00';
$PH = 7;
$TU = 'Huh What?';
$RO = 'yes';
if(($PH > 14) || ($PH < 0)) {
echo "error";
}
else {
$document = array(
"Date" => $d,
"Time" => $t,
"PH" => $PH,
"Turbidity" => $TU,
"Reduced Oxidation" => $RO
);
$db->WaterMoneteringSystem->insert($document);
echo "Document inserted successfully";
}
When I went to the database, everything was there as it should be:
The only things that I did, really was to remove the quotes from the variables and just changed the way that it connects to the database. Neither of those should have affected anything, though, so I'm not sure. Try pasting my code into your script and see if that works.
I did, however, actually go back and use your exact code (with the exception of commenting out the select a database
line and defining my own $_POST
variables, since I do not have them) and it also inserted the record just fine.
That being said, it's got to be something with another part of your script that's wonky. Are you able to select records from the DB? Meaning, is your connection set up properly and you don't have to specify a host, username or password? I'd suggest, checking there for problems.
Also, are you getting the "Document inserted successfully" message on your page? When I ran your exact code, I got this:
Connection to database successfullyDatabase project selectedCollection selected succsessfullyDocument inserted successfully
Check to make sure your message looks like that as well.
Upvotes: 1