dousi1906 2013-02-11 03:23
浏览 71
已采纳

MySQL PHP - substr()导致0个零在空表单字段中输入数据库

I am using substr(trim('SOME_FORM_VALUE'),0,7), on some form values just before submitting them to the database.

Form fields that have been submitted blank, are ending up as 0 (ZERO) in the database. If I remove the substr() everything is fine and there is no value in the database. The database (InnoDB) column is set as VARCHAR and not null.

code example:

$data_Arr = array(
'SOME_TABLE_COLUMN1' => substr(trim('SOME_FORM_VALUE1'),0,7),
'SOME_TABLE_COLUMN2' => substr(trim('SOME_FORM_VALUE2'),0,50)
);

$this->db->update('SOME_TABLE', $data_Arr);

Last MySQL Query ends up looking like this:

INSERT INTO `SOME_TABLE` (`SOME_TABLE_COLUMN1`, `SOME_TABLE_COLUMN2`)
VALUES (0, 0);
  • Why is 0 (ZERO) the result of substr() on an empty form string?
  • Any idea how to prevent the 0 (ZERO) from being entered?

Thanks all :)

  • 写回答

3条回答 默认 最新

  • douzhi4830 2015-01-30 08:53
    关注

    Used 'mb_strimwidth' - does NOT resolve to zero in the event of empty string.

    <?php
    echo mb_strimwidth("Hello World", 0, 10);
    // outputs Hello W
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退