doufeng3602 2015-12-08 19: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 19: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.

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

报告相同问题?

悬赏问题

  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)