duansengcha9114
duansengcha9114
2016-06-22 16:59

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 dougan1465 5年前

    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.

    点赞 评论 复制链接分享
  • douqiandai4327 douqiandai4327 5年前

    You are using BOOLEAN as data type. So in that case it only allows you to use 0 either 1.

    If TRUE then its 1
    If FALSE then its 0
    

    Your DB Structure

    enter image description here


    SQL General Data Types

    enter image description here

    点赞 评论 复制链接分享