doucai9270 2014-04-30 09:38
浏览 160
已采纳

如何为字符串设置默认值?

I have a page that imports fields from a csv file and imports them into my database. Before the fields can be imported into my database I need to separate the name field into two separate values (first name & last name). I do it like so:

$name = $order[5];
$fname = sqlSafe(trim(substr($name, 0, strrpos($name, " "))));`
$lname = sqlSafe(trim(substr($name, strrpos($name, " "))));`

This seems to work reasonably well, but sometimes the name field has been left partially complete (they've entered just their first name or last name for example). This results $fname being blank and mysql won't allow me to set a default value for a BLOB/TEXT column.

To solve this I have tried a couple of if statements including:

if (!$fname) {$fname = "(blank)";}
if ($fname = "") {$fname = "(blank)";}
if ($fname = NULL) {$fname = "(blank)";}

None of which seem to catch it. Where am I going wrong?

  • 写回答

5条回答 默认 最新

  • dongzhi4470 2014-04-30 09:40
    关注

    The single = in your ifs will actually not test but assign a value. Use ==or better === to test with if.

    More info on test operators: http://www.php.net/manual/en/language.operators.comparison.php

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度