简体   繁体   English

试图从$ _SESSION foreach循环中获取索引PHP

[英]Trying to get index from a $_SESSION foreach loop PHP

I am making a shopping cart and I am at the end of the payment process.The problem I have is trying to get the names in the shopping cart into my database. 我正在制作购物车,但付款流程即将结束。我遇到的问题是尝试将购物车中的名称输入数据库。 Every time I do it only the last item gets entered in instead of the whole thing if there is more then 1.Also wondered if there is a way to enter into the SQL row based on the number of the item.Ex of what i am trying to achive. 每次我这样做时,如果还有1项,那么只会输入最后一项而不是整个项目,还想知道是否有一种基于项目编号进入SQL行的方法。努力达到目的。 In the cart there is apple,orange,banana 购物车中有苹果,橙子,香蕉

apple ->order1 苹果-> order1

orange->order2 橙色-> order2

banana->order3 香蕉-> order3

What I get 我得到什么

apple->doesn't go in 苹果->不进去

orange->doesn't go in 橙色->不进

banana->order1 香蕉-> order1

PHP 的PHP

 if(isset($_SESSION['shopping_cart'])){

    //keep track of how mnay products are in the shopping cart
    $count = count($_SESSION['shopping_cart']);

    //create sequantial array for matching array keys to products id's
    $row_ids = array_column($_SESSION['shopping_cart'], 'id');

    if (!in_array(filter_input(INPUT_GET, 'id'), $row_ids)){
    $_SESSION['shopping_cart'][$count] = array
        (
            'id' => filter_input(INPUT_GET, 'id'),
            'name' => filter_input(INPUT_POST, 'name'),
            'price' => filter_input(INPUT_POST, 'price'),
            'quantity' => filter_input(INPUT_POST, 'quantity')
        );   
    }
    else {echo="do nothing";}
        }
    }

}

if (isset($_POST['payment_process'])) {

foreach ($_SESSION['shopping_cart'] as $key => $product) {
        echo  "<div>".$product['name']."</div>";
    }

}
$payment = "INSERT INTO `payment_info`
    (order1,order2,order3)
    VALUES
    ('".$product['name']."','".$product['name']."','".$product['name']."');";
    mysqli_query($db,$payment);

Your code is complicated for nothing and the way your database is structured, you're limited to 3 items per order and/or payment. 您的代码很简单,没有什么复杂的事情,而且数据库的结构方式也很复杂,每个订单和/或付款限制为3个项目。

You should have a structure of invoice -> invoice_lines: 您应该具有发票的结构-> invoice_lines:

if(isset($_SESSION['shopping_cart']) && isset($_POST['payment_process'])){

  //keep track of how many products are in the shopping cart if you need
  $count = count($_SESSION['shopping_cart']);

  $invoice = "INSERT INTO `invoices`
    (date, customer)
    VALUES
    (time(), $_SESSION['customer']);";
  mysqli_query($db,$invoice);
  $new_invoice = mysqli_insert_id($db);

  foreach ($_SESSION['shopping_cart'] as $key => $product) {
    $invoice_line = "INSERT INTO `invoice_lines`
      (invoice, id, quantity, price)
      VALUES
      ($new_invoice, $product['id'], $product['quantity'], $product['price']);";
    mysqli_query($db,$invoice_line);
  }
}
else {
  echo="do nothing";
}

Obviously, you will need to reshape your database structure accordingly and add required code to calculate invoice's total. 显然,您将需要相应地重塑数据库结构并添加所需代码以计算发票总额。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM