我试图插入一个大数组,但它打破了,因为一个字段(product_name),因为单引号"'"
如何在将数组插入数据库之前仅pSQL product_name字段?
这是我的数据库看起来像(它是一个prestshop order_detail表)
[75] => Array
(
[id_order_detail] => 76
[id_order] => 23
[id_order_invoice] => 13
[id_warehouse] => 0
[id_shop] => 1
[product_id] => 5191
[product_attribute_id] => 0
[id_parent] => 5191
[parent_reference] => 0
[item_position] => 0
[product_name] => AMIX MR POPPER'S NOCAMIX WHITE CHOCO CREAM 275G
[product_quantity] => 1
[product_quantity_in_stock] => 1
[product_quantity_refunded] => 0
[product_quantity_return] => 0
[product_quantity_reinjected] => 0
[product_price] => 8.363636
[reduction_percent] => 10.00
[reduction_amount] => 0.000000
[reduction_amount_tax_incl] => 0.000000
[reduction_amount_tax_excl] => 0.000000
[group_reduction] => 0.00
[product_quantity_discount] => 9.380000
[product_ean13] =>
[product_upc] => 647
[product_reference] => L655
[product_supplier_reference] =>
[product_weight] => 0.000000
[tax_computation_method] => 0
[tax_name] =>
[tax_rate] => 0.000
[ecotax] => 0.000000
[ecotax_tax_rate] => 0.000
[discount_quantity_applied] => 0
[download_hash] =>
[download_nb] => 0
[download_deadline] => 0000-00-00 00:00:00
[total_price_tax_incl] => 8.280000
[total_price_tax_excl] => 7.530000
[unit_price_tax_incl] => 8.280000
[unit_price_tax_excl] => 7.530000
[total_shipping_price_tax_incl] => 0.000000
[total_shipping_price_tax_excl] => 0.000000
[purchase_supplier_price] => 5.150000
[original_product_price] => 8.363636
[original_wholesale_price] => 0.000000
)
[76] => Array
(
我是这样插入的
Db: getInstance()→插入(order_detail,响应美元);
而$response是整个数组。你们都看到了,我是个新手;如有任何帮助,不胜感激。
谢谢!
您可以使用Db::getInstance()->execute编写整个查询因此,您可以转义单个字段,或者必须清理$response数组中的数据。
这里有一个简单的技巧。有了这个函数,你可以很容易地做到这一点,并且它将在更多的情况下工作。
Db::getInstance()->insert('order_detail', mypSQL($response));
function mypSQL($var)
{
if (is_array($var)) {
foreach ($var as $key => $val) {
$var[$key] = mypSQL($val);
}
} elseif (is_string($var)) {
$var = pSQL($var);
}
return $var;
}