Home>Article>Backend Development> Let’s take a look at how to implement product addition to shopping cart function in PHP (1)

Let’s take a look at how to implement product addition to shopping cart function in PHP (1)

coldplay.xixi
coldplay.xixi forward
2020-07-17 17:21:04 1830browse

Let’s take a look at how to implement product addition to shopping cart function in PHP (1)

Today I am practicing shopping cart and submitting orders. I feel a little dizzy when I write it. Let me organize it by the way. This shopping cart is relatively simple. It is used for short-term storage and is not stored in the database. , the shopping cart is simply familiar to people who love online shopping. Before writing the shopping cart, we must first conceive it. We need to call up a table from the database. Here I use the fruit table. , followed by the login table. I use the login table to adjust the user name and password. After everything is ready, there are three situations when you need to consider putting it into the shopping cart:

The first situation:There is nothing in the shopping cart

The second situation:There is already this product in the shopping cart. What to consider when adding it again is The quantity should be 1

The third situation:There is a product in the shopping cart, but there is no such product

Related learning recommendations:PHP programming from Beginner to master

The following picture is the database table used:

The following is the code for the login page:

 

登录

用户名:


密 码:


After the login page is written, you need to enter the processing page and retrieve the user name and password from the database:

Query($sql); if($arr[0][0]==$pwd && !empty($pwd)) //判断所填写的密码和取到的密码是一样的,而且密码不能为空 { $_SESSION["uid"]=$uid; header("location:main.php"); } else { echo"登录失败"; }

The login page is as shown in the figure:

Now we are going to enter the main page, retrieve all the fruit information from the database, and then we will implement the function of adding to the shopping cart.

大苹果购物网

Query($sql); foreach($arr as $v) { echo" // 从数据库调出我们所需要的内容 //这里的购买相当于添加购物车的功能 "; } ?> Query($sql1); foreach($danjia as $n) { $sum=$sum + $n[0]*$k[1]; } } echo"购物车有{$zhonglei}种商品,总价格为{$sum}元"; ?>
代号 水果名称 水果价格 原产地 货架 库存量
{$v[0]}{$v[1]} {$v[2]} {$v[3]} {$v[4]} {$v[5]} 购买

查看购物车 浏览商品 查看账户

The main page is as shown in the picture:

The most important thing is to add the shopping cart page


      

This way it can be displayed The shopping cart page, the shopping cart page code is as follows:

购物车中有以下商品:

Query($sql,1); foreach($att as $n) { echo"";} } ?>
商品名称 商品单价 购买数量
{$n[1]} {$n[2]} {$v[1]} 删除

查看购物车 浏览商品 查看账户

14 15

Enter the shopping cart page as shown in the figure:

This is just relatively simple Add to the shopping cart, but there are still many links in the middle that are not perfect. For example, after adding to the shopping cart, the number of products in the database is reduced, and the deletion of products in the shopping cart has not yet been done. These operations will be completed later.

The above is the detailed content of Let’s take a look at how to implement product addition to shopping cart function in PHP (1). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete