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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵