【问题标题】:How to insert session array data of all in to database PHP如何将所有会话数组数据插入数据库PHP
【发布时间】:2019-04-11 15:02:12
【问题描述】:

我正在开发购物车项目,一切正常。我想问一下,如何将所有购物车产品一个一个地插入到数据库中

下面是我尝试的代码,但它只插入第一个会话行而不是全部插入。

代码如下:

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

请告诉我如何将所有购物车值插入数据库。

提前致谢。

【问题讨论】:

  • header() in loop 在这里是一个问题,它会在第一次迭代中重定向你成功或失败
  • 如何解决这个问题,在哪里使用header?
  • 你收到这个Someting is wrong in updating your Information, Please try again later. ??
  • 没有我得到信息更新成功'一行成功插入,但第一行后没有插入行
  • 去掉header会解决你的INSERT问题,所有数据都会被插入

标签: php mysqli shopping-cart


【解决方案1】:

您在循环中使用header(),这将在第一次迭代中重定向成功或失败。

您可以将成功或失败状态存储在变量中

if ($update) {
    $status = 1;
} else {
    $status = 0;
}

然后,将条件移到循环之外,如下所示:

if($status) // your success
{
    header('your location');
    exit;
}
else{ // failure
    header('your location');
    exit;
}

确保$status 在顶级声明中声明为$status = 0;

请注意,您的代码对 SQL 注入是开放的,以防止 SQL 注入使用 PDO

有用的链接:

How can I prevent SQL injection in PHP?

Are PDO prepared statements sufficient to prevent SQL injection?

【讨论】:

  • 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 [价格] => 5000 [数量] => 1 [hotel_picture] => hotel2.jpg [入住] => 04/04/2019 [结账] => 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 ) ) 我在会话数组中有 2 行,但它只插入 1
  • 先生头问题现在解决了,我的循环插入会话数组的第一行两次。如何插入两行
  • @MuhammadTanzeelArshad: print_r($_SESSION["shopping_cart"]) 并分享结果
  • print_r($_SESSION["shopping_cart"]) 的结果是: 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 [数量] => 1 [hotel_picture] => hotel4.jpg [入住] => 05/02/2019 [结账] => 05/24/2019 ) )
  • @MuhammadTanzeelArshad:在 mysqli_query 行之前使用 echo $sql."<br/>"; 并分享结果
猜你喜欢
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多