簡體   English   中英

將購物車從會話轉移到數據庫

[英]Transfer Cart from Session to Database

我正在用php / mysql編寫程序,已登錄的用戶可以在其中通過我存儲在會話中的購物車訂購商品。 我想要實現的是從會話中獲取此購物車信息,並在用戶提交訂單時將其放入我的數據庫中。 我的系統沒有任何付款信息。 我的PHP知識是基礎到中級。

我的購物車頁面通過以下內容來顯示用戶購物車中的商品。

if($_SESSION['cart']) { //if the cart isn't empty
    //show the cart
    echo "<table width=\"100%\" border=\"0\" padding=\"3\" align=\"center\">";  //format the cart using a HTML table
        echo "<tr>";
            echo "<th align=\"center\"></th>";
            echo "<th align=\"center\">Quantity</th>";
            echo "<th align=\"center\">Item</th>";
            echo "<th align=\"center\">Price</th>";
        echo "</tr>";

        //iterate through the cart, the $product_id is the key and $quantity is the value
        foreach($_SESSION['cart'] as $item_id => $quantity) {   

            //get the name, description and price from the database - this will depend on your database implementation.
            //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
            $sql = sprintf("SELECT title, price FROM food WHERE food_id = %d;", $item_id); 

            $result = mysql_query($sql);

            //Only display the row if there is a product (though there should always be as we have already checked)
            if(mysql_num_rows($result) > 0) {

                list($title, $price) = mysql_fetch_row($result);

                $line_cost = number_format($price * $quantity, 2);      //work out the line cost
                $total = number_format($total + $line_cost, 2);         //add to the total cost

                echo "<tr>";
                    //show this information in table cells
                    echo "<td align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=remove&id=$item_id\" data-role=\"button\" data-icon=\"minus\" data-iconpos=\"notext\" data-transition=\"none\">Remove Item</a></td>";
                    echo "<td align=\"center\">$quantity</td>";
                    echo "<td>$title</td>";
                    //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
                    echo "<td align=\"center\">$$line_cost</td>";
                echo "</tr>";

            }

        }

        //show the total
        echo "<tr>";
            echo "<th colspan=\"3\" align=\"right\" >Total</th>";
            echo "<td align=\"center\">$$total</td>";
        echo "</tr>";

        //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
        echo "<tr>";
            echo "<td colspan=\"4\" align=\"right\"><a href=\"/delete_cart.php\" data-role=\"button\" data-icon=\"delete\" data-iconpos=\"left\" data-rel=\"dialog\">Empty Cart</a></td>";
        echo "</tr>";

        echo "<tr>";
            echo "<td colspan=\"4\" align=\"center\"><a href=\"/order.php\" data-role=\"button\">Order</a></td>";
        echo "</tr>";
    echo "</table>";

}else{
    //otherwise tell the user they have no items in their cart
    echo "<p align=\"center\">You have no items in your shopping cart.</p>";

}

然后,在訂購頁面上,我再次檢索購物車信息,並從用戶那里獲得更多詳細信息。 以下是將訂單信息提交到我的訂單表的代碼。

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "submit_order")) {
$insertSQL = sprintf("INSERT INTO orders (user_id, `date`, total, info) VALUES (%s, %s, %s, %s)",
                   GetSQLValueString($_POST['user_id'], "int"),
                   GetSQLValueString($_POST['date'], "text"),
                   GetSQLValueString($_POST['total'], "text"),
                   GetSQLValueString($_POST['info'], "text"));

mysql_select_db($database_ngs_canteen, $ngs_canteen);
$Result1 = mysql_query($insertSQL, $ngs_canteen) or die(mysql_error());

$insertGoTo = "/ngs_canteen/submit.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

我在將購物車會話中的項目輸入數據庫時​​遇到麻煩。 我在此頁面上插入用戶ID,總價,訂購日期的其他表格效果很好,但是我一生都無法弄清楚如何提交購物車商品。

我已經嘗試過類似的操作,但到目前為止仍未成功。

$result = @mysql_query($query);
if (@mysql_affected_rows($dbc) == 1) {

// Get the Order ID.
$oid = @mysql_insert_id($dbc);

// Insert the specific order contents into the database.
$query = "INSERT INTO order_items (order_id, food_id, quantity, price) VALUES (";foreach ($_SESSION['cart'] as $item_id => $value) {
$query .= "$oid, $food_id, {$value['quantity']}, {$value['price']})";
}

任何幫助將不勝感激,即使是正確方向的指點。 如果您需要任何其他信息,請詢問。

謝謝,傑克。

對於在將來經過很多修改之后遇到與我自己相同的問題的人,下面的代碼使事情對我有用。

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "submit_order")) {
$insertSQL = sprintf("INSERT INTO orders (user_id, `date`, total) VALUES (%s, %s, %s)",
                   GetSQLValueString($_POST['user_id'], "int"),
                   GetSQLValueString($_POST['date'], "text"),
                   GetSQLValueString($_POST['total'], "text"));

mysql_select_db($database_ngs_canteen, $ngs_canteen);
$Result1 = mysql_query($insertSQL, $ngs_canteen) or die(mysql_error());

$order_id = mysql_insert_id();

if ($order_id) {
foreach ($_SESSION['cart'] as $item_id => $quantity) {
    $sql = sprintf("SELECT food_id, price FROM food WHERE food_id = %d;", $item_id); 
            $result = mysql_query($sql);
            if(mysql_num_rows($result) > 0) {
                list($food_id, $price) = mysql_fetch_row($result);
                $line_cost = number_format($price * $quantity, 2);                  
            }   
$query = sprintf("INSERT INTO order_items (order_id, food_id, quantity, cost) VALUES (%s, %s, %s, %s)",
    GetSQLValueString("$order_id", "int"),
    GetSQLValueString("$food_id", "int"),
    GetSQLValueString("$quantity", "int"),
    GetSQLValueString("$line_cost", "text"));

mysql_select_db($database_ngs_canteen, $ngs_canteen);
$Result1 = mysql_query($query, $ngs_canteen) or die(mysql_error());
}}

$insertGoTo = "/ngs_canteen/submit.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

調用此代碼后,曾經存儲在會話中的用戶購物車現在將完美地轉移到您的數據庫中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM