如何使用PHP插入阵列的排列在phpMyAdmin [英] How to insert array of array in phpmyadmin using php

查看:162
本文介绍了如何使用PHP插入阵列的排列在phpMyAdmin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入行的数据库中的阵列和阵列中的每一行包含3另一个行。

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); 

以上将插入行的数组。每行包含3行,将在新表中插入

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

我的意思是有多个行需要在表格中插入 actual_regular 每行包含3个行。这3个行插入表 more_regular

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.

推荐答案

使用单一插入查询三个插入操作是这样的:

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');

这篇关于如何使用PHP插入阵列的排列在phpMyAdmin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆