2016-05-15 22 views
0

我正在構建電子商務網站。我在購物車中遇到問題。如果1位顧客從我的電子商務網站購買2本書,那麼只有1本書插入到我的表中。此外,重複值正在插入。請解決這個問題。我的電子商務網站購物車中的所有圖書都未插入到數據庫表中。

//cart.php

<?php include_once("header.php");?> 
<div id="products_box"> 
<form action="" method="post" enctype="multipart/form-data"> 

<table align="center" width="700" bgcolor="#CCFFCC"> 
<br> 
<tr align="center"> 
    <td style="font-family:'Adobe Garamond Pro', 'Times New Roman'; font-size:20px" colspan="4"><h2> Your Book(s) Details </h2></td> 
</tr> 

<tr align="center" style="font-size:20px; font-family:'Adobe Garamond Pro', 'Times New Roman'"> 
<th>Remove</th> 
<th>Book(s)</th> 
<th>Quantity</th> 
<th>Total Price</th> 
</tr> 

<?php 
$total=0; 
global $con; 
$ip = getIp(); 
$sel_price = "select * from cart where ip_add='$ip'"; 
$run_price = mysqli_query($con, $sel_price) or die(mysqli_error($con)); 

    while($p_price = mysqli_fetch_array($run_price)) 
{ 
    $b_id = $p_price['cb_id']; 
    $b_price = "select * from books where b_id='$b_id'"; 
    $run_book_price = mysqli_query($con, $b_price) or die(mysqli_error($con)); 

    while($pp_price = mysqli_fetch_array($run_book_price)) 
    { 
    $b_price = array($pp_price['b_price']); 
    $b_title = $pp_price['b_title']; 
    $b_image = $pp_price['b_image']; 
    $single_price = $pp_price['b_price']; 

    $values = array_sum($b_price); 
    $total += $values; 

    if(isset($_POST['update_qty'])) 
{ 
    if(isset($_POST['qty' . $b_id])) 
    { 


    $qty = (int)$_POST['qty' . $b_id]; 
    $run_update = "update cart set qty='$qty' where cb_id='$b_id'"; 
    $run_qty = mysqli_query($con, $run_update) or die(mysqli_error($con)); 
    $total = $total*$qty; 

    } 
} 

?> 

<tr align="center"> 
<td><input type="checkbox" name="remove[]" value="<?php echo $b_id; ?>"/> </td> 
<td><h3><?php echo $b_title; ?></h3> 
<img src="admin_area/books_images/<?php echo $b_image; ?>" width="100" height="100" style="border: ridge"/> 
</td> 
<td><input type="text" name="qty<?php echo($b_id); ?>" size="8" value=" 
    <?php 
    $default_qty=1; 
    if(!isset($_POST['qty'])) 
    { 
    echo $default_qty; 
    } 
    ?>"/></td> 

<td> <?php echo "Rs.\n". $single_price; ?></td> 
</tr> 

<?php } }?> 

<tr align="right"> 
    <td colspan="4"><b> Sub Total: </b></td> 
    <td colspan="4"><?php echo "Rs.\n". $total; ?></td> 
    </tr> 


    <tr> 
    <td align="left" style="padding-left:12px"><input type="submit" name="remove_pro" value="Remove" style="background: #000000; font-family:'Adobe Garamond Pro Bold', 'Adobe Garamond Pro', 'Adobe Caslon Pro'; font-size:18px; color:#FFFFFF"/></td> 


    <td align="center" style="padding-right:20px"><input type="submit" name="continue" value="Continue Shopping" style="background: #000000; font-family:'Adobe Garamond Pro Bold', 'Adobe Garamond Pro', 'Adobe Caslon Pro'; font-size:18px; color:#FFFFFF" /></td> 


    <td align="center" style="padding-left:12px"><input type="submit" name="update_qty" value="Update Quantity" style="background: #000000; font-family:'Adobe Garamond Pro Bold', 'Adobe Garamond Pro', 'Adobe Caslon Pro'; font-size:18px; color:#FFFFFF"/></td> 


    <td align="right" colspan="4"><input type="submit" name="checkout" value="Checkout" style="background: #000000; font-family:'Adobe Garamond Pro Bold', 'Adobe Garamond Pro', 'Adobe Caslon Pro'; font-size:18px; color:#FFFFFF; margin-right:10px"/></td> 
    </tr> 

</table> 

</form> 


<?php 

    global $con; 
    $ip = getIp(); 

    if(isset($_POST['remove_pro'])) 
{ 
    foreach($_POST['remove'] as $remove_id) 
{ 
    $delete_pro = "delete from cart where cb_id='$remove_id' AND ip_add='$ip'"; 
    $run_query = mysqli_query($con, $delete_pro) or die(mysqli_error($con)); 
    if($run_query) 
    { 
    echo "<script>window.open('cart.php','_self')</script>"; 
    } 
} 
} 

if(isset($_POST['continue'])) 
{ 
echo "<script>window.open('index.php','_self')</script>"; 
} 

if(isset($_POST['checkout'])) 
{ 
echo "<script>window.open('checkout.php','_self')</script>"; 
} 


?> 

</div> 


</div> 



</div> 
<!--Content Wrapper Ends---> 


<?php include_once("footer.php");?> 
<!--Main Wrapper Ends--> 


</body> 

</html> 

//cod.php //現金分發頁上

<?php include_once("header.php");?> 

<div id="products_box"> 
<br> 
<h2 style="color:#CD5C5C; font-family:'Adobe Garamond Pro', 'Times New Roman'; font-size:35px; padding-right:80px;"> Your Order Details are: </h2><br> 

<form method="get" action="" enctype="multipart/form-data"> 

<?php 
$total=0; 
global $con; 
$ip = getIp(); 
$sel_price = "select * from cart where ip_add='$ip'"; 
$run_price = mysqli_query($con, $sel_price) or die(mysqli_error($con)); 

    while($p_price = mysqli_fetch_array($run_price)) 
{ 
    $b_id = $p_price['cb_id']; 
    $b_price = "select * from books where b_id='$b_id'"; 
    $run_book_price = mysqli_query($con, $b_price) or die(mysqli_error($con)); 

    $b_id2 = $p_price['cb_id']; 
    $b_qty = "select * from cart where cb_id='$b_id2'"; 
    $run_qty2 = mysqli_query($con, $b_qty) or die(mysqli_error($con)); 


    $b_ip = $p_price['ip_add']; 
    $b_qty = "select * from customers where customer_ip='$b_ip'"; 
    $run_qty3 = mysqli_query($con, $b_qty) or die(mysqli_error($con)); 

    $i=0; 
    while($pp_price = mysqli_fetch_array($run_book_price)) 
    { 
    $b_price = array($pp_price['b_price']); 
    $b_id = $pp_price['b_id']; 
    $b_title = $pp_price['b_title']; 
    $b_image = $pp_price['b_image']; 
    $b_deliverychar = $pp_price['b_deliverychar']; 
    $single_price = $pp_price['b_price']; 

    $values = array_sum($b_price); 
    $total += $values; 

    while($bb_qty = mysqli_fetch_array($run_qty2)) 
    { 
    $b_qty = $bb_qty['qty']; 

    while($bb_cust = mysqli_fetch_array($run_qty3)) 
    { 
    $c_name = $bb_cust['customer_name']; 
    $c_address = $bb_cust['customer_address']; 
    $c_city = $bb_cust['customer_city']; 
    $c_state = $bb_cust['customer_state']; 


    if(isset($_POST['update_qty'])) 
{ 
    if(isset($_POST['qty' . $b_id])) 
    { 
    $qty = (int)$_POST['qty' . $b_id]; 
    $run_update = "update cart set qty='$qty' where cb_id='$b_id'"; 
    $run_qty = mysqli_query($con, $run_update) or die(mysqli_error($con)); 
    $total = $total*$qty; 
    } 
} 

?> 

<h3 style="font-family:'Adobe Caslon Pro Bold', 'Adobe Garamond Pro'; color: #0000FF; text-align:left; font-size:23px; padding-left:-8px; padding-top:90px;"><?php echo $b_id; ?>&nbsp;)&nbsp; Title:&nbsp;<?php echo $b_title; ?></h3> 

<img src="admin_area/books_images/<?php echo $b_image; ?>" width="200" height="200" style="border: ridge" align="left"/> 

<h3 style="font-family:'Adobe Caslon Pro Bold', 'Adobe Garamond Pro'; color:#0000FF; font-size:23px; padding-right:420px; padding-top:30px;"><?php if($b_qty==0) 
{ 
    echo "Quantity: 1"; 
} 
    else 
    { 
    echo "Quantity:\n" . $b_qty; 
    } ?><h3/> 
<h3 style="font-family:'Adobe Caslon Pro Bold', 'Adobe Garamond Pro'; color:#0000FF; font-size:23px; padding-right:290px; padding-top:10px;"><?php echo "Delivery Charges: Rs.\n" . $b_deliverychar; ?><h3/> 

<h3 style="font-family:'Adobe Caslon Pro Bold', 'Adobe Garamond Pro'; color: #FF0000; font-size:30px; padding-right:440px; padding-top:40px;"><?php echo "Rs.\n". $single_price; ?></h3> 

<?php } } } }?> 

    <br /> 
    <br /> 
    <br /> 

    <div class="info"> 
    <h3 style="font-family:'Adobe Caslon Pro Bold', 'Adobe Garamond Pro'; color:#006633; text-align:center; font-size:26px; padding-right:90px;"> <?php echo "Name: \n" . $c_name; ?></h3><br /> 

    <h3 style="font-family:'Adobe Caslon Pro Bold', 'Adobe Garamond Pro'; color:#006633; text-align:left; font-size:22px; padding-bottom:9px; padding-left:10px;"> 
    <?php echo "Address:\n" . $c_address; ?></h3> 


    <h3 style="font-family:'Adobe Caslon Pro Bold', 'Adobe Garamond Pro'; color:#006633; text-align:left; font-size:22px; padding-bottom:30px; padding-bottom:9px; padding-left:10px;"> 
    <?php echo "City:\n" . $c_city; ?></h3> 

    <h3 style="font-family:'Adobe Caslon Pro Bold', 'Adobe Garamond Pro'; color:#006633; text-align:left; font-size:22px; padding-bottom:30px; padding-left:10px;"> 
    <?php echo "State:\n" . $c_state; ?></h3> 
</div> 


    <div class="subtotal"> 
<b style="font-family:'Adobe Caslon Pro Bold', 'Adobe Garamond Pro'; color: #FF0000; font-size:30px;">Total: 
    <?php echo "Rs.\n". $total; ?> </b> 
    </div> 

    <div class="confirm"> 
    <input type="submit" name="confirm" value="Place Order" style="background: #0000FF; font-family:'Adobe Garamond Pro Bold', 'Adobe Garamond Pro', 'Adobe Caslon Pro'; font-size:25px; color:#FFFFFF; cursor:pointer" /></td> 
    </div> 
    </div> 
    </form> 

<?php 
if(isset($_GET['confirm'])) 
{ 
$confirm=$_GET['confirm']; 

$insert = "insert into cod (cust_name, book_name, price, total_price, image, date, delivery_char, qty, address, city, state) values ('$c_name', '$b_title', '$single_price', '$total', '$b_image', now(), '$b_deliverychar', '$b_qty', '$c_address', '$c_city', '$c_state')"; 

$insert_con = mysqli_query($con, $insert) or die(mysqli_error($con)); 

if($insert_con) 
{ 
    echo "<script>alert('Order Placed Successfully')</script>"; 
    echo "<script>window.open('confirm_order.php','_self')</script>"; 
    exit(); 
} 
} 

訂購頁//confirm_order.php //確認

<?php include_once("header.php");?> 
<div id="products_box"> 
<?php 

$get_cod="select * from cod"; 
$run_cod=mysqli_query($con, $get_cod) or die(mysqli_error($con)); 

while($row_cod=mysqli_fetch_array($run_cod)) 
{ 
$cod_id=$row_cod['cod_id']; 
$cust_name=$row_cod['cust_name']; 
$book_name=$row_cod['book_name']; 
$price=$row_cod['price']; 
$total_price=$row_cod['total_price']; 
$image=$row_cod['image']; 
$date=$row_cod['date']; 
$delivery_char=$row_cod['delivery_char']; 
$qty=$row_cod['qty']; 
$address=$row_cod['address']; 
$city=$row_cod['city']; 
$state=$row_cod['state']; 

echo " 
     <div id='single_product'> 

     <h3>Customer Name: \n $cust_name</h3> 
     <h3> Book: \n $book_name</h3> 
     <h3> Price: \n Rs. $price</h3> 
     <img src='admin_area/books_images/$image' width='200' height='200' /> 
     <h3> Total Price: Rs. \n $price</h3> 
     <h3> Order Placed on: \n $date</h3> 
     <h3> Delivery Charges:\n $delivery_char</h3> 
     <h3> Quantity:\n $qty</h3> 
     <h3> Address:\n $address</h3> 
     <h3> City:\n $city</h3> 
     <h3> State:\n $state</h3>  



     </div> 





"; 

} 
?> 
</div> 
</div> 
<?php include_once("footer.php");?> 
</body> 

</html> 

回答

0

更改

$total = $total*$qty; 

$total += $total*$qty; 
+0

我做了已經,但就是不回答。 –

+0

你是什麼意思? –

相關問題