简体   繁体   English

如何遍历INSERT INTO语句

[英]How do I loop through an INSERT INTO statement

The problem I am having is getting the INSERT INTO Mysql statement to fire during each loop of the while statement. 我遇到的问题是在while语句的每个循环期间触发INSERT INTO Mysql语句。

It is correctly looping according to the echo coming back the correct amount of times, however the INSERT INTO is only working the 1 time. 它根据echo传正确次数的echo正确循环,但是INSERT INTO仅工作1次。

Basically the question is: "How do I loop through this SQL INSERT statement using a PHP or SQL statement" 基本上,问题是:“如何使用PHP或SQL语句遍历此SQL INSERT statement语句”

Thanks in advance 提前致谢

EDIT : Being an absolute doughnut I didn't realize I had set a primary key for the orderID, which was working all along. 编辑:作为一个绝对的甜甜圈,我没有意识到我已经为orderID设置了主键,一直在工作。 It was just not allowing duplication of the orderID . 只是不允许重复orderID。

Thanks for the help 谢谢您的帮助

<?php
if(isset($_GET['checkout']))
{
$sql="SELECT * FROM cart WHERE userID =".$_SESSION['user'];
$query= mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($query)) {
            $querys= "INSERT INTO `orderdetail`(`orderID`, `selectionID`) VALUES (LAST_INSERT_ID(),{$row['cardchoiceID']})";
              $performit = $conn->query($querys);
                echo("testing");
            }
}
?>

You could optimize the approach by inserting from a select statement (call it a "migration" from one table to another): 您可以通过从select语句插入(将其称为从一个表到另一个表的“迁移”)来优化方法:

"INSERT INTO orderdetail(selectionID, orderID)
  SELECT selectionID,orderID FROM cart WHERE userID =".$_SESSION['user'];

Reference: Mysql Doc 参考: Mysql Doc

Ka_lin's answer is a fine one but your order didn't store the user/cart detail? Ka_lin的回答很好,但是您的订单没有存储用户/购物车详细信息? I see no relation between them 我没有看到他们之间的关系

Normally the flow of cart is 通常,购物车的流量为

  1. User add some items to the cart (cart has id) 用户向购物车中添加一些商品(购物车有ID)
  2. User checkout and cart become an order (generate an order id with items from the cart) with 用户结帐和购物车成为订单(使用购物车中的商品生成订单ID)

    insert into orders(cart_id,item_id,item_qty,item_price,create_datetime) select cart_id,item_id,item_qty,item_price,now() from carts;

  3. then update cart_status to marked this cart as 'finished' to make sure we create a new cart when this user returned 然后更新cart_status以将该购物车标记为“完成”,以确保我们在该用户返回时创建新的购物车

    update carts set cart_status = 'ordered' where user_id = {$user_id};

  4. Go on and process the order! 继续处理订单!

=============== ===============

Edited after saw an OP comments 看到OP评论后进行编辑

Maybe this way will work for you 也许这种方式对你有用

  1. Have a order_header (store generic info - like order_type,addresses,timestamp and users) and order_detail (store items/products related info) tables 拥有一个order_header (存储常规信息,例如order_type,地址,时间戳和用户)和order_detail (存储项目/产品相关信息)表
  2. Insert a carts info into order_header to generate an order_id 将购物车信息插入order_header以生成order_id
  3. Obtain order_id from order_header with select order_id from order_header where user = {$user_id} and status = 'new' order_header获取order_id,并从order_headerselect order_id from order_header where user = {$user_id} and status = 'new'
  4. insert item/product detail into order_detail with insert into order_detail (order_id,some columns here...) select ('{$order_id}',some columns here...) from carts where user = {$user_id}; 插入项目/产品细节成order_detailinsert into order_detail (order_id,some columns here...) select ('{$order_id}',some columns here...) from carts where user = {$user_id};
  5. then update the cart_status to 'ordered' - that's it! 然后将cart_status更新为“有序”-就是这样!

Had set orderID to primary key. 已将orderID设置为主键。 cant have duplicates 不能重复

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

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