douhan0562 2015-10-21 14:15
浏览 61
已采纳

PHP购物车在一个id中有多个值

I made a php webshop with a shopping cart for my school project. The cart is working fine but I have some problems with storing the products from the cart into the database with the orders.

I'm now making a query where the products are stored in the order database and the order_items database. But when I store them in the order database all my products get a different order_id, I want to create a query to store the products in one order_id. But for some reason I can't get that working for me.

The function in the script as I have it now:

 function cart() {
global $lang;
global $query;
global $query2;
$total = 0;
$total2 = 0;
foreach($_SESSION as $name => $value) {
    if ($value>0)   {

        if (substr($name, 0, 5)=='cart_')   {
            $id = substr($name, 5, (strlen($name)-5));
            $get = mysql_query('SELECT id, partnr, specs, price FROM parts WHERE id='.mysql_real_escape_string($id));
            while ($get_row = mysql_fetch_assoc($get))  {
                $sub = $get_row['price']*$value;
                echo $get_row['partnr'].' x '.$value.' @ &euro;'.number_format($get_row['price'], 2). ' = &euro;'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?toevoegen2='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a> <br/>';
                if(isset($_POST['behandelen'])){
                    $partnr=$get_row['partnr'];
                    $price=$get_row['price'];
                    $datum=date("Y-m-d H:i:s");
                    $query = "UPDATE parts set hoeveelheid = hoeveelheid - '$value' WHERE id = '$id'";
                    $query2= "INSERT INTO order_items(partnr, price, hoeveelheid, betaaldatum)VALUES('$partnr', '$price', '$value', '$datum')";
                }
                mysql_query($query);
                mysql_query($query2);
            }

        }
        $total += $sub; //totaalprijs exclusief btw
        $total2 += $sub*1.21; //berekening inclusief btw van 21%
        $btw = $total2-$total;
    }
}
if ($total==0)  {
    echo $lang['CART_EMPTY'];
}
else {
    global $lang;

    echo $lang['CART_SUBTOTAL'], '&euro;'.number_format($total, 2); echo "<br/>";
    echo $lang['CART_BTW'], '&euro;'.number_format($btw, 2); echo "<br/>";
    echo $lang['CART_TOTAL'], ' &euro;'.number_format($total2, 2); //prijs inclusief btw wordt weergegeven op de site
    echo "<form method='post'>
    <input type='submit' name='behandelen' value='"; echo $lang['BUTTON_PAY']; echo "'>
</form>";
}


}

The tables I have now:

Orders: link to orders image

order_items: link to order_items image

I hope someone can show me how it is done.

Thanks in advance.

Updated script:

function cart() {
global $lang;
global $query;
global $query2;
global $query3;
$total = 0;
$total2 = 0;
$datum=date("Y-m-d H:i:s");
$query3= "INSERT INTO orders (betaaldatum) VALUES ('$datum')";
$iid = mysql_insert_id();
mysql_query($query3);
foreach($_SESSION as $name => $value) {
    if ($value>0)   {

        if (substr($name, 0, 5)=='cart_')   {
            $id = substr($name, 5, (strlen($name)-5));
            $get = mysql_query('SELECT id, partnr, specs, price FROM parts WHERE id='.mysql_real_escape_string($id));

            while ($get_row = mysql_fetch_assoc($get))  {
                $sub = $get_row['price']*$value;
                echo $get_row['partnr'].' x '.$value.' @ &euro;'.number_format($get_row['price'], 2). ' = &euro;'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?toevoegen2='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a> <br/>';
                if(isset($_POST['behandelen'])){
                    $partnr=$get_row['partnr'];
                    $price=$get_row['price'];
                    $datum=date("Y-m-d H:i:s");
                    $query = "UPDATE parts set hoeveelheid = hoeveelheid - '$value' WHERE id = '$id'";
                   // $query3= "INSERT INTO orders (betaaldatum) VALUES ('$datum')";
                   // $iid = mysql_insert_id();
                    $query2= "INSERT INTO order_items(order_id, partnr, price, hoeveelheid, betaaldatum)VALUES('$iid','$partnr', '$price', '$value', '$datum')";
                }
                mysql_query($query);

                mysql_query($query2);
            }

        }
        $total += $sub; //totaalprijs exclusief btw
        $total2 += $sub*1.21; //berekening inclusief btw van 21%
        $btw = $total2-$total;
    }
}
if ($total==0)  {
    echo $lang['CART_EMPTY'];
}
else {
    global $lang;

    echo $lang['CART_SUBTOTAL'], '&euro;'.number_format($total, 2); echo "<br/>";
    echo $lang['CART_BTW'], '&euro;'.number_format($btw, 2); echo "<br/>";
    echo $lang['CART_TOTAL'], ' &euro;'.number_format($total2, 2); //prijs inclusief btw wordt weergegeven op de site
    echo "<form method='post'>
    <input type='submit' name='behandelen' value='"; echo $lang['BUTTON_PAY']; echo "'>
</form>";
}


}
  • 写回答

2条回答 默认 最新

  • dpjj4763 2015-10-21 14:20
    关注

    You have to insert your order and get the last id inserted mysql_insert_id

    Then use the returned id to identify order_id in order_items table.

    So, you need to have:

    create order table;

    before insert products in orders_items table, create one order: INSERT INTO order (date,...) VALUES (now(),...);

    recover last inserted id: $order_id = mysql_insert_id();

    insert products in order_items: INSERT INTO order_items (order_id,...) VALUES ('$order_id',...)

    You have to change:

    $query3= "INSERT INTO orders (betaaldatum) VALUES ('$datum')";
    $iid = mysql_insert_id();
    mysql_query($query3);
    

    To:

    $query3= "INSERT INTO orders (betaaldatum) VALUES ('$datum')";
    mysql_query($query3);
    $iid = mysql_insert_id();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历