im developing shopping cart project , where everything works fine . i would like to ask i thing that how to insert all cart products in to database one by one
below is the code which i try but it only insert first session row not inserting all.
here is the code:
$User_Email=$_SESSION['User_Email'];
$date=date("d-m-Y");
foreach($_SESSION["shopping_cart"] as $v){
$sql = "INSERT INTO reservation (check_in,check_out,room_id,hotel_id,User_Email,date)
values
('{$v['Checkin']}','{$v['Checkout']}','{$v['room_id']}','{$v['room_id']}','$User_Email','$date')";
$update = mysqli_query($connection, $sql);
if ($update) {
$_SESSION['success'] = 'Information updated successfully';
header("location: my_account.php");
exit;
} else {
$_SESSION['errormsg'] = 'Someting is wrong in updating your Information, Please try again later.';
header("location: my_account.php");
exit;
}}
please tell me how to insert all cart values in to database.
thanks in advance.
You are using header()
in your loop, this will redirect in first iteration either success of failure.
You can store success or failure status in an variable
if ($update) {
$status = 1;
} else {
$status = 0;
}
Then, move your condition outside your loop, as like:
if($status) // your success
{
header('your location');
exit;
}
else{ // failure
header('your location');
exit;
}
Make, sure $status
declare as $status = 0;
at top level declaration.
Note that, your code is wide open for SQL injection, for preventing SQL injection use PDO
Useful links:
How can I prevent SQL injection in PHP?
Are PDO prepared statements sufficient to prevent SQL injection?