2017-05-08 2 views
1

Ich bin beschäftigt, ein Einkaufswagen php Projekt zu erstellen, und ich habe erfolgreich abgeschlossen Hinzufügen, Entfernen und Aktualisieren von Elementen in einem Einkaufswagen, aber der Teil, an dem ich feststecke ist, wenn ein Benutzer auf "Weiter" klickt "Ich möchte, dass alles, was im Warenkorb ist, in der Datenbank entsprechend reduziert wird. Zum Beispiel, wenn eine Person hat in seinem Warenkorb: 2 x Produkt ein 3 x Produkt zweiArtikelmenge verringern in MySQL Warenkorb

ich, dass in der Menge zu verringern, möchte ich zur Hand haben, wenn der Verkauf abgeschlossen ist. Könnte jemand hier Code mein Warenkorb unter bitte helfen:

<?php 
session_start(); 
@mysql_connect("localhost","root","") or die("Could not connect to database"); 
@mysql_select_db("bookstore") or die("Could not select database"); 
include("admin/php/myFunctions.php"); 

if(!empty($_GET['prodid'])){ 
    $pid = $_GET['prodid']; 
    $wasFound = false; 
    $i = 0; 
    if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){ 
     $_SESSION["cart_array"]=array(0=>array("productID"=>$pid,"quantity"=>1)); 
    }else{ 
     foreach($_SESSION["cart_array"] as $each_product){ 
      $i++; 
      while(list($key,$value)=each($each_product)){ 
       if($key=="productID" && $value==$pid){ 
        array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$pid,"quantity"=>$each_product ['quantity']+1))); 
        $wasFound=true; 
       } 
      }  
     } 
     if($wasFound==false){ 
      array_push($_SESSION["cart_array"],array("productID"=>$pid,"quantity"=>1)); 
     } 
    } 
    header("location:shoppingcart.php"); 
    exit(); 
} 
//------------------------------------------------------------------------------------------------- 
$submit = $_POST['btnUpdate']; 
if($submit == "Update"){ 
    $x = 0; 

    foreach($_SESSION["cart_array"] as $each_product){ 
     @$i++; 
     $quantity = $_POST['txtQuan'.$x]; 
     $prodStock = $_POST['txtHoldQuan'.$x]; 
     $prodAdjustId = $_POST['txtHoldProdId'.$x++]; 
     if($quantity<1){ $quantity = 1; } 
     if($quantity>$prodStock){ $quantity = $prodStock; } 
     while(list($key,$value)=each($each_product)){ 
      array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$prodAdjustId,"quantity"=>$quantity))); 
     }  
    } 


} 
//------------------------------------------------------------------------------------------------- 
if(!empty($_GET['cid']) || isset($_GET['cid'])){ 
    $removeKey = $_GET['cid']; 
    if(count($_SESSION["cart_array"])<=1){ 
     unset($_SESSION["cart_array"]); 
    }else{ 
     unset($_SESSION["cart_array"]["$removeKey"]); 
     sort($_SESSION["cart_array"]); 
    } 
} 
//------------------------------------------------------------------------------------------------- 
$cartTitle = ""; 
$cartOutput = ""; 
$cartTotal = ""; 
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){ 
    $cartOutput="<h2 align='center'> Your shopping cart is empty </h2>"; 
}else{ 
    $x = 0; 
    $cartTitle .= '<form name="shoppingcart_form" action="shoppingcart.php" method="post" /><table width="700px" cellspacing="0" cellpadding="5"> 
      <tr bgcolor="#CCCCCC"> 
         <th width="220" align="left">Image </th> 
         <th width="140" align="left">Name </th> 
         <th width="100" align="center">Quantity </th> 
         <th width="60" align="center">Stock </th> 
         <th width="60" align="right">Price </th> 
         <th width="60" align="right">Total </th> 
         <th width="90"> </th></tr>'; 
    $i = 0; 
    foreach($_SESSION["cart_array"] as $each_product){ 
     $product_id = $each_product['productID']; 
     $sql=mysql_query("select * from tblproduct where prod_id='$product_id' limit 1"); 
     while($row=mysql_fetch_array($sql)){ 
      $prodNo = $row["prod_no"]; 
      $prodID = $row["prod_id"]; 
      $prodName = $row["prod_name"]; 
      $prodPrice = $row["prod_price"]; 
      $prodQuan = $row["prod_quan"]; 
     } 
     $pricetotal=$prodPrice*$each_product['quantity']; 
     $cartTotal= number_format($pricetotal+$cartTotal,2); 
     $cartOutput .= '<tr><td><img style="border: 2px solid;" src="images/product/'.$prodNo.'.jpg" width="150" height="120" /></td> 
      <td>'.$prodName.'</td> 
      <td align="center"><input type="hidden" name="txtHoldProdId'.$i.'" value="'.$prodID.'" /><input name="txtQuan'.$i.'" type="text" value="'.$each_product['quantity'].'" style="width: 40px; text-align: center" /> </td> 
      <td align="center"><input type="hidden" name="txtHoldQuan'.$i.'" value="'.$prodQuan.'" /> '.$prodQuan .' pcs</td> 
      <td align="right">R '.$prodPrice.'</td> 
      <td align="right">R '.$pricetotal.'</td> 
      <td align="center"> <a href="shoppingcart.php?cid='.$i++.'"><img src="images/remove_x.gif" alt="remove" /><br />Remove</a> </td></tr>'; 
    } 
    $_SESSION['checkoutCartTotal'] = $cartTotal; 
    $cartOutput .= '<tr> 
         <td colspan="3" align="right" height="40px">Have you modified your basket? Please click here to <input class="btn_upd" type="submit" name="btnUpdate" value="Update" />&nbsp;&nbsp;</td> 
         <td align="right" style="background:#ccc; font-weight:bold"> Total: </td> 
         <td colspan="2" align="left" style="background:#ccc; font-weight:bold;">R '.$cartTotal.' </td> 
         <td style="background:#ccc; font-weight:bold"> </td> 
        </tr> 
       </table> 
       <div style="float:right; width: 215px; margin-top: 20px;"> 

        <div class="checkout"><a href="checkout.php" class="more">Proceed to Checkout</a></div> 

       </div></form>'; 
} 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Great Selling Book Store</title> 
<link href="css/slider.css" rel="stylesheet" type="text/css" /> 

<link rel="stylesheet" type="text/css" href="css/ddsmoothmenu.css" /> 

<link rel="stylesheet" type="text/css" href="css/styles.css" /> 

<script language="javascript" type="text/javascript"> 

    function clearText(field) 
    { 
     if (field.defaultValue == field.value) field.value = ''; 
     else if (field.value == '') field.value = field.defaultValue; 
    } 
</script> 

</head> 

<body id="subpage"> 

<div id="main_wrapper"> 
    <div id="main_header"> 
     <div id="site_title"><h1><a href="#" rel="nofollow">Great Selling book Store</a></h1></div> 

     <div id="header_right"> 
      <div id="main_search"> 
       <form action="products.php" method="get" name="search_form"> 
        <input type="text" value="Search" name="keyword" onfocus="clearText(this)" onblur="clearText(this)" class="txt_field" /> 
        <input type="submit" name="Search" value="" alt="Search" id="searchbutton" title="Search" class="sub_btn" /> 
       </form> 
      </div> 
     </div> <!-- END --> 
    </div> <!-- END of header --> 

    <div id="main_menu" class="ddsmoothmenu"> 
     <ul> 
      <li><a href="index.php">Home</a></li> 
      <li><a href="products.php">Books</a></li> 
      <li><a class="selected" href="shoppingcart.php">Cart</a></li> 
      <li><a href="checkout.php">Checkout</a></li> 
      <li><a href="about.php">About</a></li> 
     </ul> 
     <br style="clear: left" /> 
    </div> <!-- end of menu --> 

    <div class="cleaner h20"></div> 
    <div id="main_top"></div> 
    <div id="main"> 

     <div id="sidebar"> 
      <h3>Categories</h3> 
      <ul class="sidebar_menu"> 
       <li><a href="index.php?cat=children">Children</a></li>    
       <li><a href="index.php?cat=Horror">Horror</a></li> 
       <li><a href="index.php?cat=Thriller">Thriller</a></li> 
     </ul> 
     </div> <!-- END of sidebar --> 

     <div id="content"> 
     <?php echo $cartTitle; ?> 
     <?php echo $cartOutput; ?> 

     </div> <!-- end of content --> 
     <div class="cleaner"></div> 
    </div> <!-- END of main --> 

    <div id="main_footer"> 
     <div class="cleaner h40"></div> 
     <center> 
      Copyright © 2048 DigitalNinja 
     </center> 
    </div> <!-- END of footer --> 

</div> 


<script type='text/javascript' src='js/logging.js'></script> 
</body> 
</html> 

und hier ist eine meiner Tabelle, die ich verwende in meiner Datenbank: Sie

CREATE TABLE IF NOT EXISTS `tblproduct` (
    `prod_no` int(10) NOT NULL AUTO_INCREMENT, 
    `prod_id` int(15) NOT NULL, 
    `prod_name` varchar(100) NOT NULL, 
    `prod_descr` text NOT NULL, 
    `prod_cat` varchar(100) NOT NULL, 
    `prod_price` float NOT NULL, 
    `prod_quan` int(10) NOT NULL, 
    `date_added` datetime NOT NULL, 
    `ISBN` varchar(100) NOT NULL, 
    PRIMARY KEY (`prod_no`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; 



INSERT INTO `tblproduct` (`prod_no`, `prod_id`, `prod_name`, `prod_descr`, `prod_cat`, `prod_price`, `prod_quan`, `date_added`, `ISBN`) VALUES 
(1, 1, 'Charlie and the chocolate factory', 'prod description', 'Children', 80, 100, '2016-11-01 08:25:36', '9785811243570'), 
(2, 2, 'Frankenstein', 'Prod description', 'Horror', 120, 80, '2017-05-01 05:27:11', '9781608438037'), 
(3, 3, 'The Girl on the Train', 'Prod Description', 'Thriller', 200, 90, '2017-01-18 04:22:22', '9784062932530'); 

Antwort

0

den Auftrags Unter der Annahme, in einer anderen Tabelle gespeichert ist, und hält einen Verweis auf das Produkt, sollten Sie nur in der Lage sein, so etwas zu tun:

UPDATE tblproduct AS p 
INNER JOIN tblorderproducts AS op ON p.product_no = op.product_no 
SET p.prod_quan = p.prod_quan - op.prod_quan 
WHERE op.order_id = [the order id] 
; 

Hinweis: Dies könnte einige Probleme haben, wenn das Produkt kann erscheinen in der gleichen Reihenfolge mehr als einmal.

+0

okay, ich habe Probleme hier, wo würde ich diesen Code für alle Artikel, die in den Warenkorb ist richtig funktionieren? –

+0

Ich kann Ihnen die gesamte Website auch zeigen –

+0

@ MOHAMMEDISMAIL Sie würden diese Abfrage ausführen, nachdem/wenn der Verkauf abgeschlossen ist. – Uueerdo

0

Nach der Bestellung durch den Käufer müssen - Produkt bestellt

UPDATE `tblproduct` SET prod_quan=prod_quan-$order_count WHERE prod_id=$product_id; 

Wo $ ORDER_COUNT ist die bestellte Menge und $ product_id: einen Eintrag in der Datenbank zu machen. Führen Sie dieses Update für jedes bestellte Produkt durch.

+0

Gibt es eine Möglichkeit, einen Zähler hinzuzufügen, so dass es alles auf einmal tut? –

+0

Sie müssen dies in PHP-Code schreiben. Nach der Bestellung. –

Verwandte Themen