Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a failing Codeigniter INSERT query

The data is being sent from the front end like this:

var data = {
    'user_id':userid,
    'qid':array[qnum].qid,
    'user_ans':userAnswers[qnum].answer,
    'user_time':userTime,
    'exerciseid':exid,
    'point_scored':points
};
$.post(
    '<?php echo base_url()?>main/update_user_score',
    { myData : data },
    function(result){}
);

And in my "main" controller, I have:

$post_data = $_POST['myData'];
    
$data = array(
    'user_id' => $post_data[user_id] ,
    'qid' => $post_data[qid],
    'user_ans' => $post_data[user_ans],
    'user_time' => $post_data[user_time],
    'exerciseid' => $post_data[exerciseid],
    'point_scored' => $post_data[point_scored]
);

$this->load->model('Question_model', 'questions');
$this->questions->update_user_attempt($data);

In my Question_model / update_user_attempt:

error_log("data in model BEFORE INSERT:" . json_encode($data));
$this->db->insert('user_attempt', $data);
error_log("data in model AFTER INSERT: ");

The problem is, the data reaches (at least seems to me) the model quite fine. Here's the log entry:

[23-Apr-2012 16:04:47] data in model BEFORE INSERT:{"user_id":"5","qid":"3","user_ans":"d","user_time":"3","exerciseid":"cr1","point_scored":"35"}

BUT there is NO "after insert" log entry. The insert itself does not happen, and neither does the log entry after the insert.

I can read from the DB quite fine. So I checked in phpmyadmin the user privileges of the user in "config/database.php", and that user has ALL privileges, including INSERT.

So two questions:

  1. What is the problem? What mistake am I making?
  2. How do I even begin finding out what is going on with the insert statement? (I cannot find anything in the logs.)
  3. I am looking at xampp/apache/logs/error.log and xampp/php/logs/php_error_log. Should I be looking at some other logs?
like image 222
Samudra Avatar asked Jun 10 '26 04:06

Samudra


1 Answers

Try putting $this->db->_error_message(); after db insert

like this ...

error_log("data in model BEFORE INSERT:" . json_encode($data));
$this->db->insert('user_attempt', $data);
echo $this->db->_error_message();
error_log("data in model AFTER INSERT: ");
like image 83
e1on Avatar answered Jun 11 '26 19:06

e1on



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!