简体   繁体   English

如何将购物车中的多个项目插入到表格中?

[英]How to insert multiple items from a cart into a table?

This cart code takes multiple items from the shop page and displays them perfectly on this cart page.此购物车代码从商店页面获取多个项目,并在此购物车页面上完美地显示它们。

I'm wondering how do I transport these cart variables into a table called orders.我想知道如何将这些购物车变量传输到名为 orders 的表中。

I am stuck figuring out how to input a single instance of data into an orders table for some reason - if I can figure out this, then it should be easy how to input multiple records at once.由于某种原因,我一直在弄清楚如何将单个数据实例输入到订单表中 - 如果我能弄清楚这一点,那么如何一次输入多条记录应该很容易。

I think this is on the right track, I've echoed the below variables.我认为这是在正确的轨道上,我已经回应了以下变量。 But it only echoes out the last thing in the cart.但它只会呼应购物车中的最后一件事。

$name = $product['name'];
$quantity = $product['quantity'];
$productprice = $product['price']

I was testing how to input this string by inserting dummy data from order-process.php, which checks if a submit button is processed on cart.php.我正在测试如何通过从 order-process.php 插入虚拟数据来输入这个字符串,它检查是否在 cart.php 上处理了提交按钮。

foreach($_SESSION['shopping_cart'] as $key => $product):
?>
<tr>
<td><?php echo $product['name']; ?></td>
<td><?php echo $product['quantity']; ?></td>
<td>$ <?php echo $product['price']; ?></td>
<td>$ <?php echo number_format($product['quantity'] *
$product['price'], 2); ?></td>
</tr>

<?php
echo $name;
echo $quantity;
echo $total;

$query = "insert into products values('$name','$quantity',
'$total')";
$query_run = mysqli_query($con,$query);
?>

<form method="POST" action="order-process.php" name = "submit "
value = "submit">
</form>

<?php
session_start();
require_once('dbconfig/config.php');
if(isset($_post['submit']))
{
$SQL = "INSERT INTO orders (name, quantity, total) VALUES
('abc','10', '75')";
$result = mysql_query($SQL);
}
?>

I'd lke to learn how to insert dummythis dummy data, Ideally I would like to figure out how to extract the cart variables from cart.php and multiple insert rows for whatever the user has in that cart.我想学习如何插入 dummythis 虚拟数据,理想情况下,我想弄清楚如何从 cart.php 中提取购物车变量,并为用户在该购物车中拥有的任何内容插入多个插入行。

I'm relatively new to php, but I'm sure this is close enough to an answer as I can come up with.我对 php 比较陌生,但我相信这已经足够接近我能想到的答案了。

You don't need two php files for processing cart items, you can insert items into a products table on the same page as the cart.php.您不需要两个 php 文件来处理购物车项目,您可以将项目插入到与 cart.php 位于同一页面上的产品表中。 The next step would be to place this insert statement inside a for-loop for the variables from that cart to place every item in that cart into the products table.下一步是将此插入语句放置在用于该购物车中的变量的 for 循环中,以将该购物车中的每个项目放入产品表中。

<form method="post">
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if(isset($_POST['submit']))
{
$SQL = "INSERT INTO orders (name, quantity, total, orderdate) VALUES ('abcdef', '100', '715',  NOW())";
$result = mysqli_query($con, $SQL);
}

?> ?>

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

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