简体   繁体   中英

How to insert shopping cart date into database?

I'm doing an online Food Ordering System and it has the add to cart function. Below is my cart function code. I would like to insert the data added into the cart by customer into the database to be viewed by the admin or staff to prepare the order. I have no idea how to make it work. I am very new to PHP and this is my first project. Please kindly help me..

function cart() {
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, name, price FROM products    WHERE id='.mysql_real_escape_string((int)$id));
            while ($get_row = mysql_fetch_assoc($get)){
                $sub = $get_row['price']*$value;
                echo $get_row['name'].' x '.$value.' @ RM '.number_format($get_row['price'], 2).' = RM '.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br /><br />';
                }
            }
            $total += $sub;
        }

    } 
    if ($total==0) 
    {
        echo "Your cart is empty.";
    }
    else 
    {
        echo 'Total: RM'.number_format($total, 2);
    }

}

try this

INSERT INTO products `id`,`name`,`price` VALUES('{$id}','{$name}','{$price}),('{$id}','{$name}','{$price}')

etc for each item

of course you escape the values and I am also assuming the id is auto incremented

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM