在同一个ajax调用中更新并获取数据

时间:2013-10-03 05:36:30

标签: javascript php jquery ajax codeigniter

我正在尝试更新并从ajax调用中的数据库一次获取更新的行

JS in ready function

$("button[name='teacher_lock_exam']").on(ace.click_event, function () {
    var current_exams_id = $(this).attr('id');

    bootbox.confirm("Are you sure you want to lock the exam? <br/><br/>" +
        "Locked exam cannot be modified until exam committee unlock it.", function (result) {
        if (result) {

            lock_exam(current_exams_id);
            bootbox.alert("Your Exam has been Locked !<br/><br/> Contact with Exam committee to modify that exam again.");
        }
    });

});
function lock_exam(current_exams_id) {

    $.ajax({
        url: "teacher_internal_exam_management/lock_exam/" + current_exams_id,
        type: "POST",

        dataType: "json",
        success: function (row) {
            alert('success');
            alert(row[0].access_status);


        }

    });


}

我的teacher_internal_exam_management controller

public function lock_exam($current_exams_id)
{

    $this->load->model('teacher_internal_exam_management_model');
    $this->teacher_internal_exam_management_model->lock_exam($current_exams_id);

    echo (json_encode($this->teacher_internal_exam_management_model->get_exam_details($current_exams_id)));

}

我的teacher_internal_exam_management_model模型

function lock_exam($current_exam_id, $data)
    {
        $this->db->query("update current_exams set access_status = 'locked' where current_exams_id='".$current_exam_id."'");

    }
function get_exam_details($exam_id)
    {
        $query = $this->db->query("select * from  current_exams
                                where
                                    current_exams_id = '" . $exam_id . "'

                            ");

        return $query->result();
    }

现在ajax调用正在更新数据但是控制器中的echo不返回该行。意味着ajax的成功功能未运行。为什么这不起作用?代码中有问题吗?

2 个答案:

答案 0 :(得分:1)

模型的最后一行:

return $query->result();

http://ellislab.com/codeigniter/user-guide/database/results.html

This function returns the query result as an array of objects, or an empty array on failure.

这将返回一个对象数组

你必须适当地转换它 -

return $query->result_array();

答案 1 :(得分:-2)

根据http://www.php.net/manual/en/class.mysqli-result.php的php手册,我没有看到mysqli_result类型的result()方法。我认为你需要使用

return $query->fetch_all();

而不是

return $query->result();