duanguoyin7008 2014-03-29 00: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-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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线