duanguoyin7008
2014-03-29 00:51 阅读 43
已采纳

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 doujingjiao0015 2014-03-29 00: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.
    点赞 评论 复制链接分享