i having little issue getting data table while loop. want simple want take data table cart
cookie value table orders
matches cookie value , query tables cart extract data matches cookie value in cart table , place them in table orders_final
. . final part after querying cart table cookie value gotten order table, want place data orders_final table matches cookie value order , cart
$zomo = $_cookie['shopa']; // cookie stored in cart table , updated when transaction successful $get_products = "select * `cart` 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 when update table data after payment , data gotten 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' cookie_bought = '$zomo'"; $uii = mysqli_query($con, $dance); if ($uii){ //this variable insert want insert data gotten cart table above , insert orders_final, order table holds cookie value created during shopping cookie name shopa held in variable zomo $insert = "insert `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 `cart` cookie_value = '$zomo'"; $tipee = mysqli_query($con, $delc); if ($tipee){ perform_success(); } } } }
a better approach run fewer queries, more. instead of selecting entire table , looping on run 3 queries per iteration (which becomes lot of queries!), can use insert into...select
query instead. using transaction, it's possible ensure goes through before committing changes - don't end deleting didn't transfer properly.
the code below has been altered reduce amount of queries down 3 (and none looped!), , usage of prepared statements has been implemented.
$stmt = $con->prepare("insert orders_final (`product_id`, `cookie_value`, `trx_id`, `order_quantities`) select product_id, ?, order_quantity, ? cart cookie_value=?"); $stmt->bind_param("sss", $zomo, $r_reference, $zomo); if ($stmt->execute()) { $stmt->close(); $stmt = $con->prepare("update orders set status=?, time=?, date=?, reference=?, transaction_status=?, transaction_method=?, final_price=?, order_id=?, currency=?, referrer=? cookie_bought=?"); $stmt->bind_param("sssssssssss", $r_status, $r_time, $r_date, $r_reference, $r_transaction_status, $r_transaction_method, $r_final_price, $r_order_id, $r_currency, $r_referrer, $zomo); $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' cookie_bought = '$zomo'"; $stmt = $con->prepare("delete cart cookie_value=?"); $stmt->bind_param("s", $zomo); $stmt->execute(); $stmt->close(); }
Comments
Post a Comment