亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP develops simple shopping cart function to create database table

In the previous chapters, we introduced javascript and jquery to implement the shopping cart function.

In this chapter we will use php code to explain the shopping cart function implementation ideas to our friends.

The method is to store the products obtained from the database into an array and operate the array. Each set of records in the array is information about a product (number, price, etc.),

After clicking to purchase a product, the purchase of the product is processed in the session. If it is the first purchase, the corresponding product information is added to the session; if it is already

Purchase will increase the total price and the quantity of corresponding products. Finally, the product information in the session (that is, the products in the shopping cart) and the total price are displayed on the page.

1601.png

First, create a database test:

<?php
// 創(chuàng)建連接
$conn = new mysqli("localhost", "uesename", "password");
// 檢測連接
if ($conn->connect_error) 
{    
    die("連接失敗: " . $conn->connect_error);} 
    // 創(chuàng)建數(shù)據(jù)庫
    $sql = "CREATE DATABASE test";
        if ($conn->query($sql) === TRUE) 
        {    
        echo "數(shù)據(jù)庫創(chuàng)建成功";
        } else {    
        echo "Error creating database:" . $conn->error;
        }
    $conn->close();
?>

Then create a simple good Table, used to store product information

Just create 3 directories:

id: It is unique, type is int, and select the primary key.

name: Product name, type is varchar, length is 20.

price: Product price, type is varchar, length is 20.

<?php
$SQL = "CREATE TABLE IF NOT EXISTS `good` ( 
  `id` int(6) unsigned NOT NULL AUTO_INCREMENT, 
  `name` varchar(20) NOT NULL, 
  `price` varchar(20) NOT NULL,  
  PRIMARY KEY (`id`), 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8; "
?>

After creating the table, add a few pieces of test data

<?php
 $SQL = " INSERT INTO  'good' ('id', 'name', 'price')VALUES
 (1, '蘋果', '4999’),
 (2, '微軟', '3888’),
 (3, '戴爾', '4555’);"
?>

In this way, we have completed some preparations and can start PHP coding.

Continuing Learning
||
<?php // 創(chuàng)建連接 $conn = new mysqli("localhost", "uesename", "password"); // 檢測連接 if ($conn->connect_error) { die("連接失敗: " . $conn->connect_error);} // 創(chuàng)建數(shù)據(jù)庫 $sql = "CREATE DATABASE test"; if ($conn->query($sql) === TRUE) { echo "數(shù)據(jù)庫創(chuàng)建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
submitReset Code