从数组PHP插入表 [英] Insert into table from Array PHP

查看:146
本文介绍了从数组PHP插入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个由一些值组成的数组.我想在同一行中插入具有相同索引的数组.例如:

I have four Array consisting of some values. I want to insert Array with same index in same row. Eg:

$product = [Facebook, Twitter]; 
$sub_product = [Likes, Boost]; 
$plan = [10k, 20k];
$months = [3,6]; 

我希望桌子像这样

+----+----------+-------------+------+--------+
| id | product  | sub_product | plan | months |
+----+----------+-------------+------+--------+
| 1  | Facebook | Likes       | 10k  | 3      |
+----+----------+-------------+------+--------+
| 2  | Twitter  | Boost       | 20k  | 6      |
+----+----------+-------------+------+--------+

实现此目标以使每个数组的索引位于同一行中的最佳方法是什么?我想在HTML表中插入

What will be the best way to achieve this such that Index of each array falls in same row? I want to insert in HTML table

推荐答案

如果要一次性执行mysql_query,则

If you want to execute mysql_query in single shot then

$product = [Facebook, Twitter]; 
$sub_product = [Likes, Boost]; 
$plan = [10k, 20k];
$months = [3,6]; 
$query = 'insert into TABLE (product,sub_product,plan,months) values';
foreach( $product as $index => $col ){
    $query .= "( '".$product[$index]."', '".$sub_product[$index]."', '".$plan[$index]."', ".$months[$index]." ),";
}

$query = rtrim( $query, ',');
mysqli_query(mysql_query);

这比循环执行多个mysql_query函数要快.

This will be faster than executing multiple mysql_query function in a loop.

这篇关于从数组PHP插入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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