0
votes

I was wondering if anyone can help me save the shopping cart to a database? ive looked all online but havent found anything. im trying to save an ordering form (for a mock restaurant) to the db after the user adds the items to the cart and proceeds to the pay function which already directs them to paypal screen. So basically, im trying to save the dishes the user selects and the price/qty to the database,address along with an order id. In the database i have a table called dishes (Id,name,Description,Price and Quantity). Many Thanks below is the php session code.

<?php

session_start();

$page = 'ordering.php';


mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db ('cart') or die (mysql_error());

if (isset($_GET['add'])) {
    $quantity = mysql_query('SELECT id, quantity FROM dishes WHERE id='.mysql_real_escape_string((int)$_GET['add']));
    while ($quantity_row = mysql_fetch_assoc($quantity)){
        if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]){
            $_SESSION["cart_".(int)$_GET['add']]+='1';

        }
    }
    header('Location: '.$page) ;
}

if (isset($_GET['remove'])) {
    $_SESSION['cart_'.(int)$_GET ['remove']]--;
    header('Location: '.$page) ;
}

if (isset($_GET['delete'])) {
    $_SESSION['cart_'.(int)$_GET ['delete']]='0';
    header('Location: '.$page) ;
}



function dishes(){
    $get = mysql_query('SELECT id, name, description, price FROM dishes WHERE quantity > 0 ORDER BY id DESC');
    if (mysql_num_rows($get)==0) {
        echo "There are no dishes to display!";
    }
    else {

        while ($get_row = mysql_fetch_assoc($get)) {

            echo '<p>'.$get_row['name'].'<br />'.$get_row['description'].'<br />&euro;'.number_format($get_row['price'], 2).' <a href="cart.php?add='.$get_row['id'].'"> Add</a></p>';
        }
    }
}



function cart() {
    $total = 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, name, price FROM dishes 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.' @ &euro;'.number_format($get_row['price'], 2). ' = &euro;'.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 />';
                }
            }
            $total += $sub;
        }
    }
    if ($total == 0) {
        echo "no items.";
    }
    else {
        echo 'Total: &euro;'.number_format($total, 2).'</p>';


?>

<html>
<p> 
<form action='viewcart.php' method='POST'>
<input type='submit' name='view' value='Confirm'>
</p>

    <?php
    }
}
?>

This is the html file to display the dishes and cart.

<div class="callout">
<aside class="sidebar">
<br />
<fieldset>
<?php cart(); ?>
</fieldset>
</div>
<br />
<?php dishes (); ?>
</body>
<?php include 'footer.html'; ?>
</html>

im trying to save it but im getting a id per item and i want a id per order and also i wanted the total price but its coming back empty

here is my code for inserting into database

function orders() {

    foreach($_SESSION as $name => $value) {
        if ($value !=0) {
            if (substr ($name, 0, 5)=='cart_'){
//-5 so it = to the id number
            $id = substr($name, 5, strlen ($name)-5);   
            $get = mysql_query('SELECT id, name, price FROM dishes WHERE id='.mysql_real_escape_string((int)$id));
                while ($Get_row = mysql_fetch_assoc($get)) {


                    echo '<input type="text" name="item_name_'.$num.'" value="'.$Get_row['name'].'">';
                    echo '<input type="text" name="amount_'.$num.'" value="'.$Get_row['price'].'">';
                    echo '<input type="text" name="quantity_'.$num.'"value="'.$value.'">';
                    echo '<input type="text" name="total_'.$num.'"value="'.$total.'">';


                if(mysql_query("INSERT INTO orders (name,quantity,price) VALUES ('$name','$value','$price')"))
                echo"successfully inserted";
                else
                echo "failed";
                        }


            }   
        }
    }
}
1
I'm not seeing an insert query? you are only selecting data. Also note you are using the deprecated mysql_ functions; if you are learning, I'd avoid these altogether, and I suggest looking into prepared mysqli_ statements or PDOnomistic
i have edit the above! thanks for the reply.gggg92

1 Answers

0
votes

You need an order table and an order details table, when customers place an order, you

insert an new order with customer information and new orderid ,amount paid,....

Insert every cart item, quantity,price to the details table, with the order id

Clear session content.