PHP develops si...LOGIN

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
// 创建连接
$conn = new mysqli("localhost", "uesename", "password");
// 检测连接
if ($conn->connect_error) 
{    
    die("连接失败: " . $conn->connect_error);} 
    // 创建数据库
    $sql = "CREATE DATABASE test";
        if ($conn->query($sql) === TRUE) 
        {    
        echo "数据库创建成功";
        } 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.

Next Section
<?php // 创建连接 $conn = new mysqli("localhost", "uesename", "password"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error);} // 创建数据库 $sql = "CREATE DATABASE test"; if ($conn->query($sql) === TRUE) { echo "数据库创建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
submitReset Code
ChapterCourseware