doufeng3602 2015-12-08 11:21
浏览 59
已采纳

使用括号填充数组值以执行插入

I want to have each array value inside a paranthesis

$id   = $_POST['id'];
$test2 = array($id);
$id_list = implode(',', $test2);
$sql .= "INSERT INTO tmp (id) VALUES ({$id_list});";

for example: I'm performing an insert so the output of the list should be (5),(10),(15) not '5','10','15'

Any suggestions on how I can insert using an array?

  • 写回答

1条回答 默认 最新

  • doudu2515 2015-12-08 11:30
    关注

    MySQL's extended insert syntax is

    INSERT INTO sometable (...fields...) VALUES (value set #1), (value set #2), (value set #3), etc...
    

    Note how each value set is in its own (). You're not doing that. you're just passing a bunch of values in a SINGLE () set, which means you're providing values for fields that are NOT present in the field list.

    You need to do

    $id_list = '(' . implode('),(', $_POST['id']) . ')';
    

    producing

    (5),(10),(15)
    

    instead of

    (5, 10, 15)
    

    You also need to realize that you're vulnerable to sql injection attacks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部