【发布时间】:2018-02-18 21:05:04
【问题描述】:
我在使用 while 循环从表中获取数据时遇到了一点问题。我想做的很简单,我想从table cart 获取所有数据,并从table orders 获取与cookie 值匹配的cookie 值,并查询表cart 以提取与购物车表中的cookie 值匹配的数据并将它们放在@ 987654332@。现在这是 。现在是使用从订单表中获取的 cookie 值查询购物车表后的最后一部分,我现在想将数据放入 orders_final 表中,其中所有匹配来自订单和购物车的 cookie 值
$zomo = $_COOKIE['shopa']; // this is the cookie that is stored in the cart table and updated when the transaction is successful
$get_products = "SELECT * FROM `cart` WHERE cookie_value = '$zomo'";
$limo = mysqli_query($con, $get_products);
while($colo = mysqli_fetch_array($limo)){
$product_id = $colo['product_id'];
$order_quantity = $colo['order_quantity'];
$cookie_value = $colo['cookie_value'];
//var $dance is when i update the table with data after payment and data gotten from my payment processing company
$dance = "UPDATE `orders` SET `status`='$r_status',`time`='$r_time',`date`='$r_date',`reference`='$r_reference',`transaction_status`='$r_transaction_status',`transaction_method`='$r_transaction_method',`final_price`='$r_final_price',`order_id`='$r_order_id',`currency`='$r_currency',`referrer`='$r_referrer' WHERE cookie_bought = '$zomo'";
$uii = mysqli_query($con, $dance);
if ($uii){
//this variable insert is where i want to insert all data gotten from cart table above and insert into orders_final, where order table holds the cookie value which was created during shopping which is cookie name shopa held in the variable zomo
$insert = "INSERT INTO `orders_final`(`product_id`, `cookie_value`, `trx_id`, `order_quantities`) VALUES ('$product_id','$zomo','$r_reference','$order_quantity')";
$bena = mysqli_query($con, $insert);
if ($bena){
$delc = "DELETE FROM `cart` WHERE cookie_value = '$zomo'";
$tipee = mysqli_query($con, $delc);
if ($tipee){
perform_success();
}
}
}
}
【问题讨论】:
-
您可以通过执行
INSERT INTO..SELECT查询来避免两个查询和一个循环。一些合理的代码格式(正确缩进)也会使其更容易阅读。 -
你想做什么???
-
您已经在使用支持 prepared statements 和有限变量输入的 API,您应该使用带有占位符的参数化查询(prepared statements)来保护您的数据库免受SQL-injection !开始使用
mysqli::prepare()和mysqli_stmt::bind_param()。