0
votes

I am having an issue with updating a MySql table using codeigniter.

Basically, its inserting 'img' the characters, into the table rather than the value of the variable.

This is so strange!

Here is my model:

public function update_course_progress($progress_data) {
    $course_id  = $progress_data['course_id'];
    $user_id   = $progress_data['user_id'];
    $progress = $progress_data['progress'];
    $status = $progress_data['status'];

    $update_data = array (
        'progress'  => $progress,
        'status'    => $status,
    );
    // perform update on the matching row
    $this->db->update('training_stats', $update_data, array('course_id' => $course_id, 'user_id' => $user_id));
}

So, the issue is with 'progress' instead of inserting the value of this variable it is inserting 'img'???

So, if i var_dump $update_data i get this:

array(2) { 
            ["progress"]=> string(2) "1a" 
            ["status"]=> string(1) "i" 
         } 

Which is correct:

And if i use the profiler in CI to get the db queries, this is what I get:

 UPDATE `training_stats` 
 SET `progress` = '1a', `status` = 'i'
 WHERE `course_id` = '8'
 AND `user_id` = '2'  

Which is correct.

So WHY ON EARTH is it inserting null into the db instead of 1a.

The table structure for this column is VARCHAR(4).

progress varchar(4) NOT NULL DEFAULT '0', 

What the hell is going on? why on earth is it img input???

What can be wrong?

UPDATE:

As i was debugging, i tried an insert instead of an update, and 2 rows were inserted. The first row was the expected data, and the second row was the data with 'img' in it. Both rows were the same except for the 'progress' column, which had img inserted in the second row.

So obviously it had been updating the row with the correct data and then overwriting it with the incorrect data.

But now why are there 2 rows being inserted? There is no loop? and why is the CI profiler not logging the second query, if that is indeed what is happening

1
Are you sure the column progress exists in the table?? - CarlosCarucce
Yep, heres a dump from the table progress varchar(4) NOT NULL DEFAULT '0', - frobak
Does that query run ok directly in MySQL? - LeDoc
Yes it does, it updates the table as it should with 1a not img - frobak
@frobak is table name correct Show us your DB structure - Abdulla Nilam

1 Answers

0
votes

As of per documentation of CI I will use the more standard method of updating data.

$updateArray = array (
    'progress'  => $progress_data['progress'],
    'status'    => $progress_data['status'],
);

$whereArray = array(
    'course_id' => $progress_data['course_id'],
    'user_id' => $progress_data['user_id']
)

$this->db->set($updateArray);
$this->db->where($whereArray);
$this->db->update('training_stats');

This should do, I also think you shouldn't put extra variables for the data as you did. With such short function you really are not having any benefits sinds all data is only accessed once and seem like unnecessary to me, though opinions could vary.