I want the following code to insert the car's ID in the customer_payment
table, but it only selects 477
as the ID. I don't know why. As it can be seen in image bellow only product_id = 477
is inserted, not any other value. If I select 500 it still inserts 477.
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>";
}
*/
}
?>
Why is the correct ID not being inserting into this table?
What you are trying to do here
$select_cart = "select * from cart";
$runcart = mysqli_query($conn, $select_cart);
$cartwhile=mysqli_fetch_assoc($runcart); // here
is fetching the first entry from the 'cart' table which is always going to be same.
You can try something like this.
$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);
This query will specifically fetch data for customer of current session. The rest of the code you can use as is.