如何插入数据库3数组 [英] How to insert into database 3 arrays

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

问题描述














$ (
`id` int(11)NOT NULL auto_increment,
`order` text NOT NULL,
`price $ CREATE TABLE IF NOT EXISTS` `text NOT NULL,
`quantity` text NOT NULL,
PRIMARY KEY(`id`)
)ENGINE = MyISAM DEFAULT CHARSET = cp1256 AUTO_INCREMENT = 1;

形式:

 < form method =POSTaction =#> 
< p dir =ltr>< input type =textname =order []size =20>< / p>
< p dir =ltr>< input type =textname =price []size =20>< / p>
< p dir =ltr>< input type =textname =quantity []size =20>< / p>
< p dir =ltr>< br />< / p>
< p dir =ltr>< input type =submitvalue =Submitname =B1>< / p>
< / form>

对于我使用的2个数组:

  foreach(array_combine($ orders,$ prices)as $ order => $ price){

}

我想这样插入:
ex:



1- order - 价格 - 数量

2-订单 - 价格 - 数量

<3>订单 - 价格 - 数量



4 order - price - quantity



5 order - price - quantity

$ p





如何编辑这段代码插入3个数组

谢谢

解决方案

要做到这一点,但我不知道你的表单实际上是否返回数组。你也应该做一些输入检查,看看这些值是否是数组,并且都有相同数量的项目( count 函数)。



下面的代码只是创建一个基于三个数组的插入语句。

  //获取数组和他们的sanatize。在这个例子中,我使用硬编码。 
$ orders = array('a','b','c','d','e');
$ prices = array(1,2,3,4,5);
$ quantities = array(5,6,7,8,9);


$ query =INSERT INTO orders(order,price,quantity)VALUES;
$ comma =;

//将所有数组的数组光标移动到开头。
reset($ prices);
重置($数量);
foreach($ order作为$ order)
{
//获取值和sanatize。
$ order = mysql_real_escape_string($ order);
$ price =(float)current($ prices);
$ quantity =(int)current($ quantity);

//添加到查询。
$ query。=$ comma\\\
('$ order',$ price,$ quantity);
$ comma =,\\\
;

//移动指针(对于$ order,foreach是这样的)。
next($ prices);
next($数量);
}

//执行$查询。
var_dump($ query);


i want to insert 3 arrays into database using "foreach" or something else

sql :

CREATE TABLE IF NOT EXISTS `orders` (
  `id` int(11) NOT NULL auto_increment,
  `order` text NOT NULL,
  `price` text NOT NULL,
  `quantity` text NOT NULL,
   PRIMARY KEY  (`id`)
   ) ENGINE=MyISAM DEFAULT CHARSET=cp1256 AUTO_INCREMENT=1 ;

form :

<form method="POST" action="#">
    <p dir="ltr"><input type="text" name="order[]" size="20"></p>
    <p dir="ltr"><input type="text" name="price[]" size="20"></p>
    <p dir="ltr"><input type="text" name="quantity[]" size="20"></p>
    <p dir="ltr"><br /></p>
    <p dir="ltr"><input type="submit" value="Submit" name="B1"></p>
</form>

For 2 arrays i use :

foreach (array_combine($orders, $prices) as $order=> $price) {

}

i want to insert it like that : ex:

1- order - price - quantity

2- order - price - quantity

3- order - price - quantity

4- order - price - quantity

5- order - price - quantity

...

...

how can i edit to this code to insert 3 arrays

Thank you

解决方案

The code below shows how to do that, but I'm not sure if your form actually returns arrays. You should do some input checking too, to see if the values are indeed arrays, and do all have the same number of items (count function).

Code below just creates an insert statement based on three arrays.

// Get the arrays and sanatize them. I use hard codes ones in this example.
$orders = array('a', 'b', 'c', 'd', 'e');
$prices = array(1, 2, 3, 4, 5);
$quantities = array(5, 6, 7, 8, 9);


$query = "INSERT INTO orders (order, price, quantity) VALUES";
$comma = "";

// Move the array cursor of all arrays to the beginning.
reset($prices);
reset($quantities);
foreach ($orders as $order)
{
    // Get values and sanatize.
    $order = mysql_real_escape_string($order);
    $price = (float)current($prices);
    $quantity = (int)current($quantities);

    // Add to query.
    $query .= "$comma\n('$order', $price, $quantity)";
    $comma = ",\n";

    // Move pointer (for $orders, the foreach does this).
    next($prices);
    next($quantities);
}

// Execute $query.
var_dump($query);

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

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