dpowhyh70416 2016-04-14 09:53
浏览 137
已采纳

如何使用php在phpmyadmin中插入数组数组

I am trying to insert an array of row in database and every row of array contain 3 another rows.

foreach($type as $a=>$b)
    {
     $sql="INSERT INTO `actual_regular` (`employee`, `sex`, `caste`, `family`, `local`, `worked_month`, `incash`, `total_salary`) VALUES ('$type[$a]', '$sex_actual[$a]', '$caste_actual[$a]', '$family_actual[$a]', '$employee_actual[$a]', '$worked_month[$a]', '$cash_actual[$a]', '$salary_actual[$a]');";
mysql_query($sql); 

Above will insert array of rows. Every rows contain 3 rows that will be inserted in new table

$insert = "INSERT INTO `more_regular` (`product_detail`, `unit`, `quantity`, `price`) VALUES ('$detail_product[0]', '$unit[0]', '$quantity[0]', '$price[0]');";
     $insert = "INSERT INTO `more_regular` (`product_detail`, `unit`, `quantity`, `price`) VALUES ( '$detail_product[1]', '$unit[1]', '$quantity[1]', '$price[1]');";
     $insert = "INSERT INTO `more_regular` (`product_detail`, `unit`, `quantity`, `price`) VALUES ('$detail_product[2]', '$unit[2]', '$quantity[2]', '$price[2]');";        
mysql_query($insert); 
} // above foreach ends here

I mean there are multiple rows need to be inserted in table actual_regular every rows contain 3 more rows. These 3 rows are inserted in table more_regular.

  • 写回答

1条回答 默认 最新

  • dty5753 2016-04-14 09:58
    关注

    Use single insert query for three insert operations like this:

    <?php
    $insert = "INSERT INTO `more_regular` (`product_detail`, `unit`, `quantity`, `price`) VALUES ";
    $values = array();
    foreach ($type as $a=>$b) {
        $values[] = "('$detail_product[0]', '$unit[0]', '$quantity[0]', '$price[0]')";
        $values[] = " ( '$detail_product[1]', '$unit[1]', '$quantity[1]', '$price[1]')";
        $values[] = " ('$detail_product[2]', '$unit[2]', '$quantity[2]', '$price[2]')";
    } // above foreach ends here
    if (! empty($values)) {
        $insert .= implode(', ', $values);
    }
    mysql_query($insert);
    ?>
    

    Basically, the logic is:

    INSERT INTO TABLE (ID, NAME) VALUES
    (1,'Andrew'),
    (2,'Glenn'),
    (3,'Marvel');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题