PHP using cookies to implement the shopping cart example _php Tutorial

Source: Internet
Author: User
Shopping Cart Implementation methods are the most commonly used in several ways, cookie,session and save records to the database, let me introduce the simplest way to use cookies as a shopping cart product record repository.

PHP Shopping Cart, there are many online stores in the Internet, how they realize the shopping cart? Most websites are implemented with cookies. I also wrote a simple example for you to refer to

Simple realization of shopping cart with cookies

Database:

The code is as follows Copy Code

–phpmyadmin SQL Dump
–version 2.11.9.2

– Host: 127.0.0.1:3306
– Date Generated: December 06, 2009 02:05
– Server version: 5.1.28
–php version: 5.2.6

SET sql_mode= "No_auto_value_on_zero";


– Database: ' Shopper '

– ——————————————————–


– Table's structure ' shop '

CREATE TABLE IF not EXISTS ' shop ' (
' id ' int (one) not NULL auto_increment,
' Price ' int (one) is not NULL,
' title ' varchar (a) is not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8 auto_increment=6;


– Export the data in the table ' Shop '

INSERT into ' shop ' (' id ', ' price ', ' title ') VALUES
(1, 100, ' corn '),
(2, 200, ' soy '),
(3, 500, ' watermelon '),
(4, 900, ' wax gourd '),
(5, 800, ' rice ');

PHP Code files:

The code is as follows Copy Code

/*
Author: Simple Cottage
QQ group 1:32.3119 million (full)
QQ Group 2:50,900,416
qq2:39407****** (full) Simple cottage
qq2:8726**** Cape
*/
$conn =mysql_connect ("localhost", "root", "");
mysql_select_db ("Shopper", $conn);
mysql_query ("SET NAMES Utf8″);
$sql = "Select * from ' Shop ' WHERE 1";
$sql 2=mysql_query ($sql);

if ($_post[ok]) {
$_post[number]= (int) $_post[number];
if ($_post[number]>0) {
$idid =$_post[id];
Setcookie ("cookie_arr[$idid]", $_post[title]. "|". $_post[number]. "|". $_post[price]. "|". $_post[number]*$_post[price],time () +36000);
Header ("location:shop.php");
}else{
echo "The number of inputs is incorrect.
”;
}
}
if (Isset ($_cookie[' Cookie_arr ')) {
foreach ($_cookie[' Cookie_arr ') as $name = = $value) {
$value 2=explode ("|", $value);
echo "ID ({$name})-$value number of 2[0]-: $value 2[1]– Price: $value 2[2]– Total Price: $value 2[3]

n ";
}

}
?>


Untitled Document


while ($row =mysql_fetch_array ($sql 2)) {
?>


}
?>


Defect Analysis

Cookies are easily lost as shopping carts, and if the user clears the browser cache it is possible to lose the cookie value, so it is normal to use the cookie+ database as an example.

http://www.bkjia.com/PHPjc/633124.html www.bkjia.com true http://www.bkjia.com/PHPjc/633124.html techarticle Shopping Cart Implementation methods are the most commonly used in several ways, cookie,session and save records to the database, I would like to introduce the simplest way to use cookies as a shopping cart commodity record storage ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.