drxt70655 2013-10-02 13:06
浏览 19
已采纳

php array implode for insert

I'm passing an html array to php using jquery serializearray() function.

In php I can access the array using $_POST like

 $a = $_POST['htmlarray']

The html array, however, is an array of arrays like so

 htmlarray[] = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]

I want to format the variable $a so that I can insert all the html array values in one single insert query like

 INSERT INTO table
 (val1, val2, val3, val4, val5, val6)
  VALUES
    (1,2,3,4,5,6),
   (7,8,9,10,11,12),
    (13,14,15,16,17,18)

I know I have to use an implode function, can anyone show how this could be done.

  • 写回答

1条回答 默认 最新

  • doudu4282 2013-10-02 13:14
    关注

    I'm not quite sure what an html array is, but try the following:

    $a = $_POST['htmlarray'];
    
    // unserialize $a
    
    // build sql query up to '...VALUES '
    
    foreach ($a as $row) {
       $sql .= '(';
       $sql .= implode(',', $row);
       $sql .= ')',
    }
    

    This should iterate through the arrays and append all your rows to the string. Note, however, that this code does not take care of SQL Injections at all! Not to be meant for production.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度