【问题标题】:Why is the correct ID not being inserting into this table?为什么没有将正确的 ID 插入到该表中?
【发布时间】:2016-08-21 01:04:59
【问题描述】:

我希望下面的代码在customer_payment 表中插入汽车的ID,但它只选择477 作为ID。我不知道为什么。如下图所示,只插入了product_id = 477,没有插入任何其他值。如果我选择 500,它仍然会插入 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>";
    }
    */
}
?>

为什么没有将正确的 ID 插入到该表中?

【问题讨论】:

    标签: php mysqli


    【解决方案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);
    

    此查询将专门为当前会话的客户获取数据。 其余代码您可以按原样使用。

    【讨论】:

    • 它确实输入了已登录的客户 ID,如图 3 所示。没关系,问题在于 product_id 为什么它只选择 477 产品 ID。
    • 因为购物车表中的第一个条目的 customer_id 必须为 477。
    • 您正在插入当前会话的客户 ID,但您未根据当前 customer_id 获取数据。
    • 477 是product_id 表示汽车id
    • 您正在尝试的是从表“购物车”中获取所有数据。但是您需要做的是获取当前已登录客户相关的数据。因此,您需要将 sql 从 select * from cart 更改为 select * from cart where C_ID=$c_id 其中 $c_id 取自 $_SESSION['customer_id'];因此,您现在获取的所有数据都与“购物车”表的第一个条目中的客户 ID 相关,即 3。这就是 product_id 相同的原因,即 477
    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多