Valor_ Valor_ - 4 months ago 10x
PHP Question

Codeigniter inserts true as 0 in mysql

I can't figure out why Codeigniter inserts 0 in mysql Db even thought that value is true. Let me show you an example

This is My_model where insert happens

function insert($data,$tablename=""){
$tablename = $this->table;
return $this->db->insert_id();

This is var dump data

<b>array</b> <i>(size=23)</i>
'name' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'123123'</font> <i>(length=6)</i>
'description' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'123123'</font> <i>(length=6)</i>
'tourist_location' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'true'</font> <i>(length=4)</i>
'approved_location' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'true'</font> <i>(length=4)</i>

And this is the state in DB
enter image description here

This is my table structure
table structure

So where the hell is my code failing? Why if i insert 1 or 'true' the state in db table is 0?

If you need any additional information's, please let me know and i will provide.
Thank you for help!


You've got the type wrong on your tourist_location and approved_location data.

You are actually telling the database to insert the string "true" into an integer field. The database will be expecting a single digit and when it receives your "true" it will try to convert it into an integer. The result is 0.

Can you manipulate the $data so it's more like

$data = array(
           'name' => 'xyz',
           'description' => 'somedescription',
           'tourist_location' => 1,
           'approved_location' => 1

If you're using a form to submit this data then just have checkboxes like so

<input type="checkbox" name="tourist_location" value="1">
<input type="checkbox" name="approved_location" value="1">

If it's a select then something like this will work fine

<select name="tourist_location">
    <option value="0">No</option>
    <option value="1">Yes</option>

I hope that makes sense.