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.
First, create a database test:
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.
After creating the table, add a few pieces of test data
In this way, we have completed some preparations and can start PHP coding.