2017-06-11 12:27
浏览 23


I'm want to insert the id of every item in the cart to the database and couple this invoice line to an invoice that just has been created in the same case. at the moment if i press the button its adding a new invoice to the database and after this its adding every single product in the cart but now i need to replace the 1 in the second query with the invoice id that just has been created. How can i get this to work?

  case "apply":
  $curdate = date('Y-m-d');
  // new invoice
  $sql = "INSERT INTO invoice (user_user_id, dates) VALUES ('1', '$curdate')";
  $result = mysqli_query($con, $sql);
  // for each item in cart insert into invoiceline
  foreach ($_SESSION["cart_item"] as $item){
    $car = $item["id"];
    $sql = "INSERT INTO invoice_line (car_car_id, invoice_invoice_number) VALUES ('$car', '1')";
    $result = mysqli_query($con, $sql);


The invoice_id is not the primary but a foreign key, the table doenst have a primary key so i dont think i can use mysqli_insert_id?

图片转代码服务由CSDN问答提供 功能建议

我想将购物车中每件商品的ID插入数据库,并将此发票行与 刚刚在相同的情况下创建的发票。 at,如果我按下按钮,将新发票添加到数据库中,然后将其添加到购物车中的每个产品,但现在我需要更换第二个中的1 查询刚刚创建的发票ID。 我怎样才能使这个工作?

 $ curdate = date('Ym-d'); 
 $  sql =“INSERT INTO invoice(user_user_id,dates)VALUES('1','$ curdate')”; 
 $ result = mysqli_query($ con,$ sql); 
 foreach($ _SESSION [“cart_item”]为$ item){
 $ car = $ item [“id”]; 
 $ sql =“INSERT INTO invoice_line(car_car_id,invoice_invoice_number)VALUES('$ car',  '1')“; 
 $ result = mysqli_query($ con,$ sql); 
 unset($ _ SESSION [”cart_item“]); 


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanshan3331 2017-06-11 12:36

    You can use:

    $inserted_id = mysqli_insert_id($con);

    or, object oriented style:

    $inserted_id = $con->insert_id;

    $inserted_id will have the value of just inserted row (should be auto-increment in your table).

    So, your second query will look like:

    $sql = "INSERT INTO invoice_line (car_car_id, invoice_invoice_number) VALUES ('$car', '$inserted_id')";
    解决 无用
    打赏 举报

相关推荐 更多相似问题