php購物車原理

來源:互聯網
上載者:User

標籤:檔案   資料   os   art   io   資料庫   

<?php
/*
購物車原理
在產品展示頁面時(如 shop.php?id=888),點擊購買或添加到購物車時,根據相應的產品標識符(如 id),查詢相應的資料庫,如果查詢表示有此產品,用 $_SESSION 數組儲存相應的資訊
如下樣本,表名為 goods,欄位有 id,price,.....
*/
// 開啟會話
session_start();

//引入設定檔,包含資料庫資訊常量
require(‘dbconfig.php‘);

//串連資料庫
$link = mysql_connect(DBHOST, DBUSRE, DBPASS);
mysql_select_db(DBNAME, $link);

// 根據產品 id 查詢產品資訊
$id = intval($_GET[‘id‘]);
$sql = "select * from goods where id = {$id}";
$result = mysql_query($sql, $link);

// 判斷是否存在相應的產品,有則讀取
if(empty($result) || mysql_num_rows($result) == 0)
{
    die(‘沒有相應的產品‘);
}else
{
    $shop = mysql_fetch_assoc($result);
}

// 添加一個數量欄位
$shop[‘num‘] = 1;

// 放入購物車,如已經存在,則累加
if(isset($_SESSION[‘shoplist‘][$shop[‘id‘]]))
{
    $_SESSION[‘shoplist‘][$shop[‘id‘]][‘num‘]++;
}else
{
    $_SESSION[‘shoplist‘][$shop[‘id‘]] = $shop;
}
?>
<?php
print_r($_SESSION[‘shoplist‘]);
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.