Php to delete shopping cart products

Source: Internet
Author: User
Tags psql
This article describes how to delete a shopping cart in php, which has some reference value, interested friends can refer to this article to introduce in detail the function of deleting the shopping cart in php, which has some reference value. interested friends can refer to it.

In the previous article, I wrote a simple function to add to the shopping cart. after the products in the shopping cart are deleted and the orders are submitted, the inventory and the customer account balance are not complete, this article continues to improve the previous article. the previous article was written to the function of deleting the shopping cart. to reduce the number of deleted code, we need

When the link is deleted, the value of the retrieved ids is changed to the value of the arr index. The value of k is changed to the value of the arr index.

Delete
In this way, after entering the delete page, we can directly obtain the index value sy, which makes it much easier to delete the page.

The code for deleting the page is as follows: shanchu. php


 1) {$ arr [$ sy] [1] = $ arr [$ sy] [1]-1;} else // if the number is 1, remove {unset ($ arr [$ sy]);} $ _ SESSION ["gwc"] = $ arr; // save the content header ("location: gouwuche. php ");

In this way, you can delete the page. here is the code for submitting the page, which is relatively cumbersome. when it comes to submitting the page, we will think of inventory and balance. we only need to submit the order, then the inventory will decrease, and the relative balance will also decrease. of course, if the inventory is insufficient or the balance is insufficient, you should also inform the purchaser.

Two tables are created here to store data.

Add a tag at the end of gouwuche. php.

The following is the code of tijiaodingdd. php.

 Query ($ ysql); $ yarr [0] [0]; // Total amount // The total price of the shopping cart. $ arr = array (); if (! Empty ($ _ SESSION ["gwc"]) {$ arr = $ _ SESSION ["gwc"] ;}$ sum = 0; foreach ($ arr as $ v) {$ v [1]; // quantity of products in the shopping cart $ psql = "select price from fruit WHERE ids = '{$ v [0]}'"; $ parr = $ db-> Query ($ psql); foreach ($ parr as $ k) {$ k [0]; // product unit price $ sum + = $ k [0] * $ v [1] ;}} // Determine whether the balance meets the purchase if ($ yarr [0] [0]> = $ sum) {// if the balance is met, you need to determine the inventory foreach ($ arr as $ v) {$ ksql = "select number from fruit where ids = '{$ v [0]}'"; $ karr = $ db-> Query ($ ksql ); $ karr [0] [0]; // This is an inventory if ($ Karr [0] [0] <$ v [1]) // indicates insufficient inventory. in this case, inform the customer of insufficient inventory {echo "insufficient inventory"; exit ;}} // after judgment, the order must be submitted. // the account balance is deducted from $ kcsql = "update login set account = account-{$ sum} where username = '{$ uid }'"; $ db-> Query ($ kcsql, 0); // Here is the modify statement, so add 0 // deduct inventory foreach ($ arr as $ v) {$ kcksql = "update fruit set number = number-$ v [1] where ids = '{$ v [0]}'"; $ db-> Query ($ kcksql, 0);} // when all the work is done, we should submit the order. // Here I have made two tables in the database, add the submitted order to the table and save it. // add the order $ ddh = date ("Ymd His "); $ time = date (" Y-m-d H: I: s "); $ sdd =" insert into orders values ('{$ ddh }', '{$ uid}', '{$ time}') "; $ db-> Query ($ sdd, 0 ); // add order details foreach ($ arr as $ v) {$ sddxq = "insert into orderdetails values ('', '{$ ddh }', '{$ v [0]}', '{$ v [1]}') "; $ db-> Query ($ sddxq, 0 );}} else {echo "insufficient balance"; exit ;}?>

The shopping cart is the same as the product in the previous shopping cart. now let's look at the tables in the database.

Note:Because the zhangsan account has been used for many tests, the account price in the login table has changed to 33.6 yuan. The product price we put into the shopping cart is 16.8 yuan, so there is no problem here.

Such a simple shopping cart can be completely completed.

The above is a detailed description of the php shopping cart product deletion function (text) details, please pay attention to the first PHP community other related articles!

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.