[英]How to enter more than one value into one database column
I am trying to get a completed order to save to a table called orders in a database. 我正在尝试获取完成的订单,以保存到数据库中称为“订单”的表中。 In the database, the orders table only shows the last product, but I need it to show all the products on the order. 在数据库中,orders表仅显示最后一个产品,但我需要它来显示订单上的所有产品。
In the code below ($query2) it grabs all the ids of the products that are in the users cart.(There are two products in the cart) 在下面的代码($ query2)中,它获取用户购物车中产品的所有ID。(购物车中有两个产品)
I then use a while loop to display them, but if I try to INSERT them into the orders table($query3) using the $pro_id variable, it only grabs the last product in the cart and enters it into the database. 然后我使用while循环来显示它们,但如果我尝试使用$ pro_id变量将它们插入到订单表($ query3)中,它只会抓取购物车中的最后一个产品并将其输入数据库。
How do I get both products to enter into the orders table? 如何获得两种产品进入订单表? Do I need to use an array somehow? 我需要以某种方式使用数组吗? Or is there a different way to do this? 还是有其他方法可以做到这一点?
Thanks! 谢谢!
$ip = getIp();
$query = "SELECT * FROM cart WHERE ip='$ip'";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$pro_qty = $row['quantity'];
$query2 = "SELECT * FROM product WHERE id='$id'";
$result2 = mysqli_query($conn, $query2);
if(mysqli_num_rows($result2) > 0){
while ($row = mysqli_fetch_array($result2)){
$pro_id = $row['id'];
}
}
}
}
if(isset($_POST['placeOrder'])){
$query3 = "INSERT INTO orders (productId) VALUES ('$pro_id')";
$result3 = mysqli_query($conn, $query3);
}
}
I found someone online that showed me a different way to do what I was trying to do. 我发现网上有人向我展示了一种不同的方式来做我想要做的事情。 Instead of using an array to save both products to one column; 而不是使用数组将两个产品保存到一列; I created two tables in the database ("orders" and "orderproducts"). 我在数据库中创建了两个表(“orders”和“orderproducts”)。 Using the code below, I made it so that if the "placeOrder" button was clicked it would INSERT the customer's order into the "orders" table (with a unique number that I created and saved in the variable "$orderId"). 使用下面的代码,我做到了这样,如果单击“ placeOrder”按钮,它将把客户的订单插入“ orders”表(具有我创建并保存在变量“ $ orderId”中的唯一编号)。
I then got the id of the products that were in the customers cart and created a while loop that would run through the products in the cart and using "$query3" INSERT each product individually into its own row in the "orderproducts" table with the "orderId='$orderId'" ($orderId is the unique number I created) and "productId='$productId'". 然后,我获得了客户购物车中产品的ID,并创建了一个遍历购物车中所有产品的while循环,并使用“ $ query3”将每个产品分别插入“ orderproducts”表中自己的行中, “orderId ='$ orderId'”($ orderId是我创建的唯一编号)和“productId ='$ productId'”。
if(isset($_POST['placeOrder'])){
$query = "INSERT INTO orders (orderId) VALUES ('$orderId')";
$result = mysqli_query($conn, $query);
$ip = getIp();
$query1 = "SELECT * FROM cart WHERE ip='$ip'";
$result1 = mysqli_query($conn, $query1);
if(mysqli_num_rows($result1) > 0){
while($row = mysqli_fetch_array($result1)){
$id = $row['id'];
$query2 = "SELECT * FROM product WHERE id='$id'";
$result2 = mysqli_query($conn, $query2);
if(mysqli_num_rows($result2) > 0){
while ($row = mysqli_fetch_array($result2)){
$productId = $row['id'];
$query3 = "INSERT INTO orderproducts (orderId, productId) VALUES ('$orderId', '$productId')";
$result3 = mysqli_query($conn, $query3);
}
}
}
}
}
Then to grab that order, I made a query like this: 然后,为了抓取该订单,我进行了如下查询:
$orderNumber = 5578;(This is a fake order number)
$query = "SELECT * FROM orders WHERE orderId='$orderNumber'";
$result = mysqli_query($conn, $query);
if($result){
$query1 = "SELECT * FROM orderproducts WHERE orderId='$orderNumber'";
$result1 = mysqli_query($conn, $query1);
}
You can modify your variable $pro_id and make it an array type like this: 您可以修改变量$ pro_id并使其成为如下所示的数组类型:
$pro_id = array(); $ pro_id = array();
and then use it to store your ids in this array. 然后用它来存储你的ID在这个数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.