1

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.

1 Answers1

1

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?

devpro
  • 16,184
  • 3
  • 27
  • 38
  • Print_r of session array: Array ( [0] => Array ( [room_id] => 8 [hotel_name] => Ayub Residence [room_type_name] => Super Plus [count_of_travels] => 5 [count_of_childerns] => 3 [price] => 5000 [quantity] => 1 [hotel_picture] => hotel2.jpg [Checkin] => 04/04/2019 [Checkout] => 04/22/2019 ) [1] => Array ( [room_id] => 4 [hotel_name] => Borith Inn Hotel [room_type_name] => Deluxe [count_of_travels] => 4 [count_of_childerns] => 2 [price] => 3200 [quantity] => 1 [hotel_picture] => hotel4.jpg [Checkin] => 05/02/2019 [Checkout] => 05/24/2019 ) ) i have 2 rows in session array but it inserting only 1 – Muhammad Tanzeel Arshad Apr 11 '19 at 15:12
  • sir header problem is solved now my loop insert 1st row of session array twice . how to insert both rows – Muhammad Tanzeel Arshad Apr 11 '19 at 15:20
  • @MuhammadTanzeelArshad: `print_r($_SESSION["shopping_cart"])` and share the result – devpro Apr 11 '19 at 15:22
  • Result of print_r($_SESSION["shopping_cart"]) is: Array ( [0] => Array ( [room_id] => 8 [hotel_name] => Ayub Residence [room_type_name] => Super Plus [count_of_travels] => 5 [count_of_childerns] => 3 [price] => 5000 [quantity] => 1 [hotel_picture] => hotel2.jpg [Checkin] => 04/04/2019 [Checkout] => 04/22/2019 ) [1] => Array ( [room_id] => 4 [hotel_name] => Borith Inn Hotel [room_type_name] => Deluxe [count_of_travels] => 4 [count_of_childerns] => 2 [price] => 3200 [quantity] => 1 [hotel_picture] => hotel4.jpg [Checkin] => 05/02/2019 [Checkout] => 05/24/2019 ) ) – Muhammad Tanzeel Arshad Apr 11 '19 at 15:24
  • @MuhammadTanzeelArshad: use `echo $sql."
    ";` before mysqli_query line and share the result
    – devpro Apr 11 '19 at 15:26
  • INSERT INTO reservation (check_in,check_out,room_id,hotel_id,User_Email,date) values ('04/25/2019','04/30/2019','9','9','muneebamalik200@gmail.com','11-04-2019') it echo only 1 row – Muhammad Tanzeel Arshad Apr 11 '19 at 15:28
  • @MuhammadTanzeelArshad: this is not possible – devpro Apr 11 '19 at 15:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191672/discussion-between-devpro-and-muhammad-tanzeel-arshad). – devpro Apr 11 '19 at 15:35