PHP foreach循环SQL插入 [英] PHP foreach loop SQL insert
本文介绍了PHP foreach循环SQL插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
像这样可能吗?对于数组中的每个项目都插入数据库吗?我应该怎么做,因为显然这是行不通的.目前,它从数组中取出最后一项,并插入其数据.
Is something like this possible? For each item in array make an insert into database? How should I do it, because clearly this doesn't work. At the moment it takes last item from the array and inserts its data.
foreach ($properties as $prop):
$productProp = new ProductProperty();
$productProp->product_id = $product->ID;
$productProp->properties_id = $property->ID;
$productProp->set_properties_id = $set_property->ID;
endforeach;
$productProp->save(); //inserts into database
推荐答案
您必须为每个对象在数据库中编写.
You have to write in the database for each object.
-
创建对象
Create the object
将对象的属性设置为某些值
Set the object's attributes to certain values
将对象插入数据库
并在数组仍具有条目(对象)时重复.
And repeat while your array still have entries (objects).
代码:
foreach ($properties as $prop):
//Create a new object
$productProp = new ProductProperty();
//Set the attributes
$productProp->product_id = $product->ID;
$productProp->properties_id = $property->ID;
$productProp->set_properties_id = $set_property->ID;
//Insert it in the database
$productProp->save();
endforeach;
这篇关于PHP foreach循环SQL插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文