Delete shopping cart in php (2), and delete shopping cart in php

Source: Internet
Author: User

Delete shopping cart in php (2), and delete shopping cart in php

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.

<A href = 'shanchu. php? Sy = {$ k} '> Delete </a> </td>
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

<? Phpsession_start (); $ sy =$ _ GET ["sy"]; // locate the data based on the index $ arr =$ _ SESSION ["gwc"]; $ arr [$ sy]; // The data to be deleted // if the quantity is not 1, the quantity is reduced by 1if ($ arr [$ sy] [1]> 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.

<Div> <a href = "tijiaodd. php" rel = "external nofollow"> </a> <div>

The following is the code of tijiaodingdd. php.

<? Php session_start (); header ("Content-type: text/html; charset = UTF-8"); // prevents garbled characters $ uid = $ _ SESSION ["uid"]; // first check the account balance include ("DADB. class. php "); $ db = new DADB (); $ ysql =" select account from login where username = '{$ uid }'"; $ yarr = $ db-> 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.

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.