Detailed PHP implementation of the shopping Cart product removal function (graphic)

Source: Internet
Author: User
Tags psql
This article is mainly for everyone to introduce the PHP implementation of the shopping Cart product removal function, with a certain reference value, interested in small partners can refer to

In the last article just write a simple add shopping cart function, shopping cart product deletion, submit orders, inventory reduction and customer account balance is not perfect, this one is then perfect the last one, the last one written to the shopping cart removed the function, in order to make the deletion of the code less knock some, we have to put the front

Link Delete page When you get the value of IDs instead of the index value for arr, the index value of arr is changed to

<a href= ' shanchu.php?sy={$k} ' > Delete </a></td>
So we can get the index value directly after we get to the delete page sy so the delete page is much easier to write.

Here is the code to delete the page shanchu.php


<?phpsession_start (); $sy = $_get["sy"];//Locate the data according to 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 quantity is 1, remove {unset ($arr [$sy]);} $_session["GWC"] = $arr; Finally save the contents of the Shopping cart header ("location:gouwuche.php");

So delete the page, the following is the code to submit the page, relatively cumbersome, when referring to the submission page, we will think of the inventory, balance, we just submit the order, then the inventory will be reduced, the relative balance will be reduced, of course, if the inventory is insufficient, or the balance is insufficient, but also to the purchaser prompt.

In this place, two tables were used to store the data.

Add a label at the end of the gouwuche.php

<p><a href= "tijiaodd.php" rel= "External nofollow" ></a><p>

Here's the tijiaodingdd.php code.

<?php session_start (); Header ("Content-type:text/html;charset=utf-8"); Prevent garbled $uid=$_session["UID"];//first check the account balance include ("DADB.class.php"); $db =new dadb (); $ysql = "Select accounts from Login Where Username= ' {$uid} ' "; $yarr = $db->query ($ysql); $yarr [0][0];//total//Shopping cart Total price, preceded by $arr=array (); if (!empty ($_ session["GWC"]) {$arr =$_session["GWC"];} $sum =0;foreach ($arr as $v) {$v The number of products in the 1];//cart $psql = "Select price from fruit WHERE ids= ' {$v [0]} '"; $parr = $db->query ($ PSQL); foreach ($parr as $k) {$k [unit price of the 0];//product $sum + = $k [0]* $v [1];}} Determine if the balance is satisfied with the purchase if ($yarr [0][0]>= $sum) {//Balance satisfied, to determine 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, at this time to give customers the inventory of insufficient {echo "inventory insufficient";  Exit }}//After judging the order must be submitted//account deducted Balance $kcsql = "Update login set account=account-{$sum} where Username= ' {$uid} '"; $db->query ($kcsql, 0);//This is the modification statement, so add 0//Subtract inventory foreach ($arr as $v) {$kcksql = "update fruit set number=number-$v [1] wher E IDS= ' {$v [0]} '; $db->query ($kcksql, 0); }//all the work is done, then we should submit the order//Here I made two tables in the database, add the submitted order to the table can be saved//Add order $DDH = Date ("Ymdhis"); $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) {$SDD  XQ = "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 one in the previous shopping cart, when we look at the tables in the database.

Note: because there have been many times before the test is Zhangsan account, so the login table in the accounts price has changed to 33.6 Yuan, we put into the shopping cart product price is 16.8 yuan, so here is no problem.

Such a relatively simple shopping cart, even if completely completed.

Related Article

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.