使用PHP应用程序在MongoDB中执行CRUD操作

时间:2014-01-23 16:28:06

标签: javascript php ajax codeigniter mongodb

我是一名新手PHP和MongoDB开发人员。

我创建了一个PHP网页项目,其中包含一个包含“添加”按钮的HTML页面。该页面的名称是awards.html。 awards.html文件包含其对应的JavaScript文件awards.js。单击“添加”按钮时,将在此js文件中执行代码。此代码向名为example.php的项目中的其他位置的PHP类发送AJAX调用,该类包含执行名为connect()的函数的代码,该函数位于Awards.php文件中。

我的文件的源代码如下:

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

<?php

include 'Awards.php';

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

Awards.php

<?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
    }
}

我的项目的目的是在名为test的MongoDB数据库上执行CRUD操作。我正在使用Codeigniter-MongoDB-Library连接到此数据库。我已相应编辑了mongodb.php库文件以连接到我的数据库。此文件的配置设置如下:

mongodb.php

<?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;

这里的问题是连接功能没有被执行;换句话说,我无法成功连接到我的数据库。我怎么知道这个?因为在awards.html页面上,我得到的输出是读取,不允许直接访问脚本,这是来自mongo_db.php文件(请注意,不是我上面提到的mongodb.php文件)。

任何人都可以告诉我我哪里错了?最早的回复将受到高度赞赏。提前谢谢。

0 个答案:

没有答案
相关问题