whileループでテーブルからデータを取得する際に少し問題があります。私がやりたいことは簡単ですtable cart
からクッキー値を持つすべてのデータをtable orders
からクッキー値と照合テーブルカートに一致させて、カートテーブルのクッキー値と一致するデータを抽出してtable orders_final
に配置したいと考えています。今は
です今、最後の部分は、オーダーテーブルから得たクッキー値をカートテーブルを照会した後、私は今、そのクッキーの値に一致するすべてでorders_finalテーブルにデータを配置するため、カート
からテーブルからデータを取得し、whileループから別のテーブルに挿入
$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'クエリを実行して、2つのクエリおよびループを回避することができます。コードを適切に字下げすると、読みやすくなります。 – Qirel
あなたは何をしたいですか? –
すでに** prepared statement **をサポートしているAPIを使用していますが、[SQL-injection](http://stackoverflow.com)からデータベースを保護するために、プレースホルダ付きのパラメータ付きクエリを準備する必要があります。/q/60174 /)! ['mysqli :: prepare()'](http://php.net/mysqli.prepare)と['mysqli_stmt :: bind_param()'](http://php.net/mysqli-stmt)から始めましょう。 .bind-param)。 – Qirel