duansengcha9114 2016-06-22 16:59
浏览 34
已采纳

Codeigniter在mysql中将true插入0

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=""){
        if($tablename=="")
            $tablename = $this->table;
        var_dump($data);
        $this->db->insert($tablename,$data);
        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>
</pre>

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!

  • 写回答

2条回答 默认 最新

  • dougan1465 2016-06-22 17:11
    关注

    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>
    </select>
    

    I hope that makes sense.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?