What to change, so that the multiple inserts are done at once and quicker












1















The problem is that I want to make one query to insert all the content of the cart at one time to the database table ordersdetail to improve the speed.



In my code below the inserts are now in a for loop using prepared statements. The script below is working, but an insert takes place now at every iteration.



I thought it could be quicker if I put the whole cart in an array, but I got stuck. I can not fix the problem. Any help is appreciated.



THE SCRIPT



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();


EDITS



$cart = unserialize (serialize ($_SESSION ['cart'])); 


is edited in:



$cart = $_SESSION ['cart'];


@Nick. The bindstatement is moved outside the loop right after the prepare statement.










share|improve this question

























  • You can move the bind_param outside the loop (immediately after the prepare statement).

    – Nick
    Jan 1 at 21:23











  • Why are you doing $cart = unserialize (serialize ($_SESSION ['cart'])); instead of just $cart = $_SESSION ['cart'];?

    – Nick
    Jan 1 at 21:23











  • @Nick. bind_param outside the loop right after prepare statement won't work. The parameters are in the loop!

    – Tuncay
    Jan 1 at 21:44











  • you only need to use bind_param once, then just reassign the variables for each pass through the loop. See the example here

    – Nick
    Jan 1 at 22:12











  • @Nick. Thanks for your reply. I edit my code conform your advise and it works allso. Is binding the parameters outside the loop, in this case before the loop right after the prepare statement, making the inserts quicker? Isn'it better to execute them in ones? If yes, how do I do that?

    – Tuncay
    Jan 1 at 22:34
















1















The problem is that I want to make one query to insert all the content of the cart at one time to the database table ordersdetail to improve the speed.



In my code below the inserts are now in a for loop using prepared statements. The script below is working, but an insert takes place now at every iteration.



I thought it could be quicker if I put the whole cart in an array, but I got stuck. I can not fix the problem. Any help is appreciated.



THE SCRIPT



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();


EDITS



$cart = unserialize (serialize ($_SESSION ['cart'])); 


is edited in:



$cart = $_SESSION ['cart'];


@Nick. The bindstatement is moved outside the loop right after the prepare statement.










share|improve this question

























  • You can move the bind_param outside the loop (immediately after the prepare statement).

    – Nick
    Jan 1 at 21:23











  • Why are you doing $cart = unserialize (serialize ($_SESSION ['cart'])); instead of just $cart = $_SESSION ['cart'];?

    – Nick
    Jan 1 at 21:23











  • @Nick. bind_param outside the loop right after prepare statement won't work. The parameters are in the loop!

    – Tuncay
    Jan 1 at 21:44











  • you only need to use bind_param once, then just reassign the variables for each pass through the loop. See the example here

    – Nick
    Jan 1 at 22:12











  • @Nick. Thanks for your reply. I edit my code conform your advise and it works allso. Is binding the parameters outside the loop, in this case before the loop right after the prepare statement, making the inserts quicker? Isn'it better to execute them in ones? If yes, how do I do that?

    – Tuncay
    Jan 1 at 22:34














1












1








1








The problem is that I want to make one query to insert all the content of the cart at one time to the database table ordersdetail to improve the speed.



In my code below the inserts are now in a for loop using prepared statements. The script below is working, but an insert takes place now at every iteration.



I thought it could be quicker if I put the whole cart in an array, but I got stuck. I can not fix the problem. Any help is appreciated.



THE SCRIPT



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();


EDITS



$cart = unserialize (serialize ($_SESSION ['cart'])); 


is edited in:



$cart = $_SESSION ['cart'];


@Nick. The bindstatement is moved outside the loop right after the prepare statement.










share|improve this question
















The problem is that I want to make one query to insert all the content of the cart at one time to the database table ordersdetail to improve the speed.



In my code below the inserts are now in a for loop using prepared statements. The script below is working, but an insert takes place now at every iteration.



I thought it could be quicker if I put the whole cart in an array, but I got stuck. I can not fix the problem. Any help is appreciated.



THE SCRIPT



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();


EDITS



$cart = unserialize (serialize ($_SESSION ['cart'])); 


is edited in:



$cart = $_SESSION ['cart'];


@Nick. The bindstatement is moved outside the loop right after the prepare statement.







php mysqli






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 23:02







Tuncay

















asked Jan 1 at 20:15









TuncayTuncay

62




62













  • You can move the bind_param outside the loop (immediately after the prepare statement).

    – Nick
    Jan 1 at 21:23











  • Why are you doing $cart = unserialize (serialize ($_SESSION ['cart'])); instead of just $cart = $_SESSION ['cart'];?

    – Nick
    Jan 1 at 21:23











  • @Nick. bind_param outside the loop right after prepare statement won't work. The parameters are in the loop!

    – Tuncay
    Jan 1 at 21:44











  • you only need to use bind_param once, then just reassign the variables for each pass through the loop. See the example here

    – Nick
    Jan 1 at 22:12











  • @Nick. Thanks for your reply. I edit my code conform your advise and it works allso. Is binding the parameters outside the loop, in this case before the loop right after the prepare statement, making the inserts quicker? Isn'it better to execute them in ones? If yes, how do I do that?

    – Tuncay
    Jan 1 at 22:34



















  • You can move the bind_param outside the loop (immediately after the prepare statement).

    – Nick
    Jan 1 at 21:23











  • Why are you doing $cart = unserialize (serialize ($_SESSION ['cart'])); instead of just $cart = $_SESSION ['cart'];?

    – Nick
    Jan 1 at 21:23











  • @Nick. bind_param outside the loop right after prepare statement won't work. The parameters are in the loop!

    – Tuncay
    Jan 1 at 21:44











  • you only need to use bind_param once, then just reassign the variables for each pass through the loop. See the example here

    – Nick
    Jan 1 at 22:12











  • @Nick. Thanks for your reply. I edit my code conform your advise and it works allso. Is binding the parameters outside the loop, in this case before the loop right after the prepare statement, making the inserts quicker? Isn'it better to execute them in ones? If yes, how do I do that?

    – Tuncay
    Jan 1 at 22:34

















You can move the bind_param outside the loop (immediately after the prepare statement).

– Nick
Jan 1 at 21:23





You can move the bind_param outside the loop (immediately after the prepare statement).

– Nick
Jan 1 at 21:23













Why are you doing $cart = unserialize (serialize ($_SESSION ['cart'])); instead of just $cart = $_SESSION ['cart'];?

– Nick
Jan 1 at 21:23





Why are you doing $cart = unserialize (serialize ($_SESSION ['cart'])); instead of just $cart = $_SESSION ['cart'];?

– Nick
Jan 1 at 21:23













@Nick. bind_param outside the loop right after prepare statement won't work. The parameters are in the loop!

– Tuncay
Jan 1 at 21:44





@Nick. bind_param outside the loop right after prepare statement won't work. The parameters are in the loop!

– Tuncay
Jan 1 at 21:44













you only need to use bind_param once, then just reassign the variables for each pass through the loop. See the example here

– Nick
Jan 1 at 22:12





you only need to use bind_param once, then just reassign the variables for each pass through the loop. See the example here

– Nick
Jan 1 at 22:12













@Nick. Thanks for your reply. I edit my code conform your advise and it works allso. Is binding the parameters outside the loop, in this case before the loop right after the prepare statement, making the inserts quicker? Isn'it better to execute them in ones? If yes, how do I do that?

– Tuncay
Jan 1 at 22:34





@Nick. Thanks for your reply. I edit my code conform your advise and it works allso. Is binding the parameters outside the loop, in this case before the loop right after the prepare statement, making the inserts quicker? Isn'it better to execute them in ones? If yes, how do I do that?

– Tuncay
Jan 1 at 22:34












1 Answer
1






active

oldest

votes


















-1














After a quick check and it seems that it's not so easy to get the prepared statement escaped query so an alternative is to escape the data without the prepared statement, here is a quick implementation :



$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);


Warning You should add more validations regarding the data you want to insert






share|improve this answer
























  • This is not what I am looking for. Thanks for your answer. I prefer to use prepared statements.

    – Tuncay
    Jan 1 at 22:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998622%2fwhat-to-change-so-that-the-multiple-inserts-are-done-at-once-and-quicker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














After a quick check and it seems that it's not so easy to get the prepared statement escaped query so an alternative is to escape the data without the prepared statement, here is a quick implementation :



$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);


Warning You should add more validations regarding the data you want to insert






share|improve this answer
























  • This is not what I am looking for. Thanks for your answer. I prefer to use prepared statements.

    – Tuncay
    Jan 1 at 22:44
















-1














After a quick check and it seems that it's not so easy to get the prepared statement escaped query so an alternative is to escape the data without the prepared statement, here is a quick implementation :



$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);


Warning You should add more validations regarding the data you want to insert






share|improve this answer
























  • This is not what I am looking for. Thanks for your answer. I prefer to use prepared statements.

    – Tuncay
    Jan 1 at 22:44














-1












-1








-1







After a quick check and it seems that it's not so easy to get the prepared statement escaped query so an alternative is to escape the data without the prepared statement, here is a quick implementation :



$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);


Warning You should add more validations regarding the data you want to insert






share|improve this answer













After a quick check and it seems that it's not so easy to get the prepared statement escaped query so an alternative is to escape the data without the prepared statement, here is a quick implementation :



$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);


Warning You should add more validations regarding the data you want to insert







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 22:28









chas antchas ant

96




96













  • This is not what I am looking for. Thanks for your answer. I prefer to use prepared statements.

    – Tuncay
    Jan 1 at 22:44



















  • This is not what I am looking for. Thanks for your answer. I prefer to use prepared statements.

    – Tuncay
    Jan 1 at 22:44

















This is not what I am looking for. Thanks for your answer. I prefer to use prepared statements.

– Tuncay
Jan 1 at 22:44





This is not what I am looking for. Thanks for your answer. I prefer to use prepared statements.

– Tuncay
Jan 1 at 22:44




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998622%2fwhat-to-change-so-that-the-multiple-inserts-are-done-at-once-and-quicker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter