问题是我想进行一次查询以将购物车的所有内容一次插入数据库表ordersdetail
中,以提高速度。
在我下面的代码中,插入代码现在处于使用准备好的语句的for循环中。下面的脚本正在运行,但是现在在每次迭代时都会进行插入。
我认为如果将整个购物车排列成一个阵列,可能会更快,但是我被卡住了。我无法解决问题。任何帮助表示赞赏。
脚本
include_once '../../includes/db_connect.php';
class Item{
var $productid;
var $productnaam;
var $productomschrijving;
var $productprijs_excl;
var $productprijs_incl;
var $product_btw_tarief;
var $quantity;
var $lesuren;
}
session_start();
$order_id = $_SESSION["order_id"];
$klantid = $_SESSION["klantid"];
$insert_stmt = $mysqli->prepare('INSERT INTO ordersdetail (order_id, productid, productnaam, productomschrijving, productprijs_incl, product_btw_tarief, aantal, subtotaalexcl, subtotaal, klantid, lesuren) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$insert_stmt->bind_param('iissddiddid', $order_id, $productid, $productnaam, $productomschrijving, $productprijs_incl, $product_btw_tarief, $aantal, $subtotaalexcl, $subtotaal, $klantid, $lesuren);
$cart = $_SESSION ['cart'];
for($i = 0; $i < count($cart); $i++){
$productid = $cart[$i]->productid;
$productnaam = $cart[$i]->productnaam;
$productomschrijving = $cart[$i]->productomschrijving;
$productprijs_incl = $cart[$i]->productprijs_incl;
$product_btw_tarief = $cart[$i]->product_btw_tarief;
$subtotaalexcl = $cart[$i]->productprijs_excl * $cart[$i]->quantity;
$subtotaal = $cart[$i]->productprijs_incl * $cart[$i]->quantity;
$aantal = $cart[$i]->quantity;
$lesuren = $cart [$i]->lesuren * $cart[$i]->quantity;
$insert_stmt->execute();
}// End for loop
$insert_stmt->close();
编辑
$cart = unserialize (serialize ($_SESSION ['cart']));
在以下位置进行编辑:
$cart = $_SESSION ['cart'];
@尼克。在准备语句之后,绑定状态语句立即移到循环外部。
答案 0 :(得分:-1)
经过快速检查后,使准备好的语句转义查询似乎并非易事,因此另一种方法是不用准备好的语句转义数据,这是一个快速实现:
$order_id = $_SESSION["order_id"];
$klantid = $_SESSION["klantid"];
$cart = unserialize (serialize ($_SESSION ['cart']));
$insert_values = '';
for($i = 0; $i < count($cart); $i++){
$productid = $cart[$i]->productid;
$productnaam = $cart[$i]->productnaam;
$productomschrijving = $cart[$i]->productomschrijving;
$productprijs_incl = $cart[$i]->productprijs_incl;
$product_btw_tarief = $cart[$i]->product_btw_tarief;
$subtotaalexcl = $cart[$i]->productprijs_excl * $cart[$i]->quantity;
$subtotaal = $cart[$i]->productprijs_incl * $cart[$i]->quantity;
$aantal = $cart[$i]->quantity;
$lesuren = $cart [$i]->lesuren * $cart[$i]->quantity;
//You can escape them during assigning, I did it here to easily see the data types
//i
$order_id = (int)$order_id;
//i
$productid = (int)$productid;
//s
$productnaam = mysqli_real_escape_string($mysqli, $productnaam);
//s
$productomschrijving = mysqli_real_escape_string($mysqli, $productomschrijving);
//d
$productprijs_incl = (double) $productprijs_incl;
//d
$product_btw_tarief = (double) $product_btw_tarief;
//i
$aantal = (int)$aantal;
//d
$subtotaalexcl = (double)$subtotaalexcl;
//d
$subtotaal = (double)$subtotaal;
//i
$klantid = (int) $klantid;
//d
$lesuren = (double) $lesuren;
$insert_values .= "($order_id, $productid, $productnaam, $productomschrijving, $productprijs_incl, $product_btw_tarief, $aantal, $subtotaalexcl, $subtotaal, $klantid, $lesuren),";
}// End for loop
//trim trailing ,
rtrim($insert_values, ",");
$sql = "INSERT INTO ordersdetail (order_id, productid, productnaam, productomschrijving, productprijs_incl, product_btw_tarief, aantal, subtotaalexcl, subtotaal, klantid, lesuren) VALUES " . $insert_values;
$mysqli->query($sql);
警告,您应该添加有关要插入的数据的更多验证