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条)

报告相同问题?

悬赏问题

  • ¥50 神舟笔记本,没有linux的驱动,装的Ubuntu系统,想把风扇速度调到最大
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥15 应用商店如何检测在架应用内容是否违规?
  • ¥15 Ubuntu系统配置PX4
  • ¥50 nw.js调用activex
  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题