duanguoyin7008 2014-03-28 16:51
浏览 49
已采纳

PHP - 如果字段在数组中更新字段

I'm working on a mailbox system for a game on Facebook. I have 2 inputs to a php script, with example input below:

$FriendIDs = "10000001,10002421,10132000,10074794,13523543"

$MailCode = "ReqGem"

and a table with the columns ID, Mailbox.

What I want to be able to do is concatenate whatever was originally in the Mailbox field, with MailCode, for each person in the FriendIDs.

I figured it was something like this, but I couldn't get it to work (my php/sql knowledge is pretty dire!):

mysqli_query($db, "UPDATE Save SET Mailbox = CONCAT(Mailbox,'$MailCode' . '_') WHERE 'id' IN $FriendIDs);

EDIT: I've just realized I need to add a new row if the FriendID isn't already in the table.

I'm guessing I need to start out with INSERT INTO and then use ON DUPLICATE KEY UPDATE, but I can't seem to get it to work. It's a bit trickier since the unique key is in an array, and I can't use WHERE id IN(ArrayOfValues) in an INSERT query.

Any help would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • doujingjiao0015 2014-03-28 16:54
    关注

    You're close:

    $FriendIDs = "10000001,10002421,10132000,10074794,13523543";
    $MailCode = "ReqGem";
    mysqli_query($db, "UPDATE Save SET Mailbox = CONCAT(Mailbox,'$MailCode' . '_') WHERE `id` IN($FriendIDs));
    
    • You just needed the parenthesis for IN() as it is a function.
    • Don't use single quotes for column names. Use ticks. Single quotes are for strings.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部