doushen8391 2016-06-08 09:18
浏览 21
已采纳

PHP - 删除额外的逗号

I have 3 post variables & i need to save those 3 variable values in single field in mysql table

below are table field details:
-------------------------------
id int(11)
name varchar(100)
country varchar(100)

details varchar(100) (this field used for store all 3 variables value)

if variable values are posted as id=1, name-steve & country=usa then needs to store as

RESULT - 1,steve,usa

now problem is all 3 variable are optional so if passed just id & name needs to store as

RESULT - 1,steve (no last comma will be store)

if passed just id & country then needs to store as

RESULT - 1,usa (no additional comma will be stored)

I have tried like:

if(isset($data['id']) || isset($data['name']) || isset($data['country']))
{
    $details=$data['id'].",".$data['name'].",".$data['country'];
}

How can i achieve this in php-mysql ?

  • 写回答

3条回答 默认 最新

  • doushi1996 2016-06-08 09:22
    关注

    Put present values in an array and implode it after

    $details = [];
    
    if(isset($data['id']) and !empty($data['id'])) $details[] = $data['id'];
    if(isset($data['name']) and !empty($data['name'])) $details[] = $data['name'];
    if(isset($data['country']) and !empty($data['country'])) $details[] = $data['country'];
    $details = implode(',', $details);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂