2016-04-26 74 views
-20

我想要下面的代碼在customer_payment表中插入汽車ID,但它只選擇477 ID。我不知道爲什麼。如圖所示,只有product_id 477被插入,如果我選擇500,它仍然會插入477.請幫助我獲得此幫助。謝謝 enter image description here爲什麼正確的ID沒有插入表

include 'admin/db.php'; 

if(isset($_GET['payment_here'])){ 

    //select product id from cart 
    $select_cart = "select * from cart"; 
    $runcart = mysqli_query($conn, $select_cart); 
    $cartwhile=mysqli_fetch_assoc($runcart); 

    $carssid = $cartwhile['P_ID']; 
    $cusid = $cartwhile['C_ID']; 

    //select id from cars 
    $scars = "select * from cars where id=$carssid"; 
    $scarsrun = mysqli_query($conn, $scars); 
    $showcars = mysqli_fetch_assoc($scarsrun); 
    $carsdealer = $showcars['dealer']; 


    //select customer id from customer table 
    //$selectcust = "select * from customer_register where id=$cusid"; 
    //insert data into customer payment table 
    echo $insertpay = "insert into customer_payment 
    (Product_id, customer_id, dealer) 
    values ($carssid," . $_SESSION['customer_id'] . ", '$carsdealer')"; 
    $run_inserts = mysqli_query($conn, $insertpay); 
    /* 
    if($run_inserts){ 
     echo "<script>window.location.href = 'checkout.php'</script>"; 
    } 
    */ 
} 
?> 

回答

1

你正在嘗試被取出由它總是將是相同的「車」表中的第一個條目這裏

$select_cart = "select * from cart"; 
$runcart = mysqli_query($conn, $select_cart); 
$cartwhile=mysqli_fetch_assoc($runcart); // here 

做。

你可以嘗試這樣的事情。

$c_id = $_SESSION['customer_id']; 
$select_cart = "select * from cart where C_ID=$c_id"; 
$runcart = mysqli_query($conn, $select_cart); 
$cartwhile=mysqli_fetch_assoc($runcart); 

該查詢將專門爲當前會話的客戶提取數據。 您可以使用的其餘代碼是。

+0

它確實輸入了登錄的客戶ID,如圖3所示。這很好,問題出在product_id上,爲什麼它只選擇了477個產品ID。 –

+0

,因爲購物車表中的**第一個條目**必須將customer_id設爲477. – nikamanish

+0

您正在從當前會話插入**客戶ID,但您沒有根據當前customer_id獲取**數據。 – nikamanish