Using cookies in php to implement shopping cart instances

Source: Internet
Author: User
The most common methods for implementing the shopping cart are cookie, session, and storing records in the database. next I will introduce the simplest method to use cookie as the product record repository of the shopping cart.

The most common methods for implementing the shopping cart are cookie, session, and storing records in the database. next I will introduce the simplest method to use cookie as the product record store of the shopping cart.

PHP shopping cart. There are many online stores on the internet. how do they implement shopping cart? Most websites use cookies, and even write a simple example for your reference.

The database code is as follows:

  1. -PhpMyAdmin SQL Dump
  2. -Version 2.11.9.2
  3. -
  4. -Host: 127.0.0.1: 3306
  5. -Generation date: December 06, 2009
  6. -Server version: 5.1.28
  7. -PHP version: 5.2.6
  8. SET SQL _MODE = "NO_AUTO_VALUE_ON_ZERO ";
  9. -
  10. -Database: 'shopper'
  11. -
  12. --------------------
  13. -
  14. -Table structure 'shop'
  15. -
  16. Create table if not exists 'shop '(
  17. 'Id' int (11) not null AUTO_INCREMENT,
  18. 'Price' int (11) not null,
  19. 'Title' varchar (110) not null,
  20. Primary key ('id ')
  21. ) ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 6;
  22. -
  23. -Export the data 'shop' in the table'
  24. -
  25. Insert into 'shop '('id', 'price', 'title') VALUES
  26. (1,100, 'corn '),
  27. (2,200, 'soybean '),
  28. (3,500, 'watermelon '),
  29. (4,900, 'melon '),
  30. (5,800, 'Rice ');

PHP code file:

  1. /*
  2. Author: simple hut
  3. QQ Group: 1: 32311900 (full)
  4. QQ Group 2: 50900416
  5. QQ2: 39407 * (full) simple hut
  6. QQ2: 8726 * cape
  7. */
  8. $ Conn = mysql_connect ("localhost", "root ","");
  9. Mysql_select_db ("shopper", $ conn );
  10. Mysql_query ("set names utf8 ″);
  11. $ SQL = "SELECT * FROM 'shop 'WHERE 1";
  12. $ Sql2 = mysql_query ($ SQL );
  13. If ($ _ POST [OK]) {
  14. $ _ POST [number] = (int) $ _ POST [number];
  15. If ($ _ POST [number]> 0 ){
  16. $ Idid = $ _ POST [id];
  17. Setcookie ("cookie_arr [$ idid]", $ _ POST [title]. "| ". $ _ POST [number]. "| ". $ _ POST [price]. "| ". $ _ POST [number] * $ _ POST [price], time () + 36000 );
  18. Header ("location: shop. php ");
  19. } Else {
  20. Echo "The number entered is incorrect.
    ";
  21. }
  22. }
  23. If (isset ($ _ COOKIE ['cookie _ arr']) {
  24. Foreach ($ _ COOKIE ['cookie _ arr'] as $ name => $ value ){
  25. $ Value2 = explode ("|", $ value );
  26. Echo "ID ({$ name})-$ value2 [0]-quantity: $ value2 [1]-unit price: $ value2 [2]-Total Price: $ value2 [3] N ";
  27. }
  28. }
  29. ?>
  30.  
  31. Untitled Document 
  32.  
  33.  
  34. While ($ row = mysql_fetch_array ($ sql2 )){
  35. ?>
  36.  
  37.  
  38. }
  39. ?>
  40.  
  41.  

Disadvantage analysis:Cookie is easily lost as a shopping cart. if the user clears the browser cache, the cookie value may be lost. therefore, generally, the cookie + database is used as an instance.

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.