EnduroDave
EnduroDave

Reputation: 1033

Incorrect value written to mysql database

I am trying to write a value to a database. Everything seems to be fine except one value is mysteriously incorrect. I just can't figure this out.

Using codeigniter here is my controller:

$sample_id = $this->input->post('sample_id');
$culture_id = $this->input->post('culture_id');
$sample_name = $this->vial_model->get_name($culture_id);
$box_id = $this->input->post('boxid');

$db_data['boxid'] = $box_id;
$db_data['taskid'] = $sample_id;
$db_data['projectid'] = $culture_id;
$vial_id = $this->vial_model->create($db_data);

$vial_link = '<a href="'.base_url('freezer_vial/view/'.$culture_id.'/'.$sample_id.'/'.$vial_id).'" >'.$sample_name.'</a>';

Lets imagine that the value for $sample_id is 158 (or any number really) and that I can echo this value to the view to confirm.

The anchor link that is output to the view is as expected and contains 158. However, the value for $db_data['taskid'] is always 127 and is inserted into the database as this. I have no idea why. Everything else works fine.

Here is the model:

public function create($data)
{
    $insert = $this->db->insert('vial', $data);
    if($insert)
        return $this->db->insert_id();
    else
        return false;
}

Upvotes: 0

Views: 78

Answers (1)

Th. Ma.
Th. Ma.

Reputation: 9464

The column in your database table holding the sample id value might have been defined as a tinyint. See also https://stackoverflow.com/a/16684301/282073.

You might want to alter the column type to ensure no data is altered for each new insertion or update. Instances can be found in another answer https://stackoverflow.com/a/13772308/282073

Official documentation to alter tables can be read from http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

Upvotes: 1

Related Questions