Page 1 of 1

Unable to Perform CRUD Operations in MongoDB Using PHP

Posted: Thu Jan 23, 2014 11:12 am
by rugved
0 down vote favorite


I am a rookie PHP and MongoDB developer.

I have created a PHP web project with an HTML page that contains an 'Add' button. The name of the page is awards.html. The awards.html file contains its counterpart JavaScript file, awards.js. A code is executed in this js file when the Add button is clicked. This code sends an AJAX call to a PHP class elsewhere in the project named, example.php which contains code to execute a function called, connect() that is in an Awards.php file.

The source-code of my files is given as follows:

Awards.html

<div class = "divbottom">
<div id="divAddAward">
<button class="btn" onclick="onrequest();">Add</button>
</div>
</div>

awards.js

function onrequest() {
$("#divAddAward").load('example.php');
alert('Test');
$.post(
'example.php'
).success(function(resp) {
json = $.parseJSON(resp);
alert(json);
});
}

example.php

Code: Select all

<?php

include 'Awards.php';

$class = new Awards();
$method =  $class->connect();
echo json_encode($method);
Awards.php

Code: Select all

<?php

require_once 'mongodb-library/libraries/Mongo_db.php';
include 'mongodb-library/libraries/Mongo_db.php';

class Awards extends Mongo_db 
{

    //put your code here

    public function __construct() 
    {
        parent::__construct();
    }

    public function connect()
    {
        $this->load();


        //The following code is used for confirmation
        $array = array(
        $this->message => '1'
        );
        return $array;
    }
    public function create()
    {
        //Code to read
    }

    public function read()
    {
        //Code to read
    }

    public function update()
    {
        //Code to update
    }

    public function delete()
    {
        //Code to delete
    }
}
The purpose of my project is to perform CRUD operations on my MongoDB database called, test. I am using the Codeigniter-MongoDB-Library to connect to this database. I have edited the mongodb.php library file accordingly to connect to my database. The config settings for this file are given as follows:

mongodb.php

Code: Select all

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['default']['mongo_hostbase'] = 'localhost:27017';
$config['default']['mongo_database'] = 'test';
$config['default']['mongo_username'] = '';
$config['default']['mongo_password'] = '';
$config['default']['mongo_persist']  = TRUE;
$config['default']['mongo_persist_key']  = 'ci_persist';
$config['default']['mongo_replica_set']  = FALSE;
$config['default']['mongo_query_safety'] = 'safe';
$config['default']['mongo_suppress_connect_error'] = TRUE;
$config['default']['mongo_host_db_flag']   = FALSE;
The problem here is that the connect function is not getting executed; in other words, I am not able to connect to my database successfully. How do I know this? Because on the awards.html page I am getting an output which reads, No direct script access allowed, which is coming from the mongo_db.php file (mind you not the mongodb.php file I have mentioned above).

Can anyone please tell me where exactly am I going wrong? Replies at the earliest will be highly appreciated. Thank you in advance.