只有1项存储在我的数据库中

时间:2016-02-28 06:55:17

标签: php

checkout.php ............... 我用这个代码吧。当我购买2件或更多件商品时,我的数据库中只存储了1件商品。提前谢谢...................

<?php
session_start();
include('conn.php');
require('item.php');
if(isset($_POST['confirm'])){
$order=$_POST['order'];
$date = date('Y-m-d H:i:s');
$lname=$_POST['lname'];
$fname=$_POST['fname'];
$mname=$_POST['mname'];
$add=$_POST['add'];
$em=$_POST['email'];
$num=$_POST['number'];
if(empty($order)){
echo"<script>alert('Please choose your payment method.')</script>";
}
mysqli_query($con,"INSERT INTO `godgets`.`order` (`Lastname`, `Firstname`, `Middlename`, `Address`, `Email`, `Number`, `Date`) VALUES ('$lname', '$fname', '$mname', '$add', '$em', '$num', '$date')");
$orderid=mysqli_insert_id($con);
$cart=unserialize(serialize($_SESSION['cart']));
for ($i=0; $i<count($cart); $i++) {
$p_name=$cart[$i]->name;
$p_price=$cart[$i]->price;
$p_des=$cart[$i]->des;
$p_quantity=$cart[$i]->quantity;
$p_image=$cart[$i]->image;
$p_total=$cart[$i]->price * $cart[$i]->quantity;
mysqli_query($con,"INSERT INTO order_details (`ORDER_ID`, `P_name`, `P_price`, `P_quantity`, `Picture`,`Total`) VALUES 
('.$orderid.', '$p_name.', '.$p_price.', '.$p_quantity.', '.$p_image.','.$p_total.')");
}
echo"<script>alert('Thankyou For Shopping!')</script>";
echo"<script>window.open('index.php','_self')</script>";
unset($_SESSION['cart']);
exit();
}
?>

请帮我,我是新手:( tnx

FDS 的 item.php

<?php
    class Item{
        var $id;
        var $name;
        var $des;
        var $quantity;
        var $price;
        var $image;
    }
?>

addcart.php

            <?php
            session_start();
            ?>
            <head>
            <meta name="viewport" content="width=device-width, initial-    scale=1">
              <link rel="stylesheet" type="text/css" href="css\bootstrap.min.css">
              <link rel="stylesheet" type="text/css" href="css\bootstrapValidator.min.css">
               <script src="jq/jquery.min.js"></script>
              <script src="js/bootstrap.min.js"></script>
              <script src="js/bootstrapValidator.min.js"></script>
            </head>
            <body background="pic/bg3.jpg" style="background-attachment:fixed;background-size:cover;">
            <?php include("navbar.php");
            require('conn.php');
            require('item.php');
            if(!isset($_SESSION['username'])){
                echo"<script>window.open('sign.php','_self')</script>";
            exit();
            }
            if(isset($_GET['id']) && !isset($_POST['update'])){
            $result=mysql_query('select * from products_db where P_ID ='.$_GET['id']);
            $product=mysql_fetch_object($result);
            $item= new Item();
            $item->id= $product->P_ID;
            $item->name=$product->P_NAME;
            $item->des=$product->DESCRIPTION;
            $item->price=$product->P_PRICE;
            $item->image=$product->PICTURE;
            $item->quantity=1;
            //check product
                $index=-1;
                $cart=unserialize(serialize($_SESSION['cart']));
                for($i=0; $i<count($cart);$i++)
                if ($cart[$i]->id==$_GET['id']){
                    $index=$i;
                    break;
                }
                if($index==-1){
                $_SESSION['cart'][]=$item;
                }
                else{
                $_SESSION['cart']=$cart;
                }
            }
            //delete
            if(isset($_GET['index']) && !isset($_POST['update'])){
            $cart=unserialize(serialize($_SESSION['cart']));
            unset($cart[$_GET['index']]);
            $cart=array_values($cart);
            $_SESSION['cart']=$cart;
            }
            if (isset($_POST['update'])){
                $arrQuantity=$_POST['quantity'];
                // check quantity
                $valid=1;
                for($i=0; $i<count($arrQuantity);$i++)
                    if(!is_numeric($arrQuantity[$i]) || $arrQuantity[$i]< 1){
                    $valid=0;
                    break;
                }
                if($valid==1){
                $cart=unserialize(serialize($_SESSION['cart']));
                for($i=0; $i<count($cart);$i++){
                    $cart[$i]->quantity=$arrQuantity[$i];
                }
                $_SESSION['cart']=$cart;
                }
                else
                    $error='Quantity is Invalid';
            }
            ?>
            <div class="container">
                <div class="row">
                    <div class="col-md-12 col-xs-12">
                        <div class="panel panel-info">
                          <div class="panel-heading">
                          <form method="POST">
                            <div class="panel-title">
                                        <h5><span class="glyphicon glyphicon-shopping-cart" style="float:left;"></span>&nbsp;YOUR ITEM(s)
                                        <label style="color:red;margin-left:400px;"><?php echo isset($error) ? $error : '';?></label>
                                        <a href="producthot.php" style="float:right;"class="btn btn-success btn-sm "><span class="glyphicon glyphicon-share-alt"></span> Continue Shopping</a>
                                        <button type="submit" name="update" style="float:right; margin-right:3%;"class="btn btn-info btn-sm "><span class="glyphicon glyphicon-floppy-saved"></span>Update Cart</button>
                                        </h5>               
                            </div>
                        </div>
                        <?php
                        $cart=unserialize(serialize($_SESSION['cart']));
                        $s=0;
                        $index=0;
                        for ($i=0;$i<count($cart); $i++){
                            $s+=$cart[$i]->price * $cart[$i]->quantity;

                        ?>
                        <div class="panel-body">
                                <div class="row">
                                    <div class="col-md-2 col-xs-12">
                                        <img class="img-responsive" src="<?php echo $cart[$i]->image;?>" style="height:30%;width:100%;">
                                    </div>
                                        <div class="col-md-4 col-xs-12">
                                            <h4 style="font-family:agency fb;"><strong><?php echo $cart[$i]->name;?></strong></h4>
                                            <div><h4 style="font-family:agency fb;"><small><?php echo $cart[$i]->des;?></small></h4></div>
                                        </div>
                                        <div class="col-md-6 col-xs-12">
                                        <div class="col-md-6 text-right">
                                                <h4 style="font-family:agency fb;"><strong>Php <?php echo $cart[$i]->price;?></strong> x </h4>                                  
                                        </div>
                                        <div class="col-md-4 col-xs-9">
                                        <input type="text" name="quantity[]" value="<?php echo $cart[$i]->quantity;?>" class="form-control" style="width:50px;">
                                        </div>
                                        <div class="col-md-3 col-xs-2" style="margin-top:1%;">
                                        <a href="addcart.php?index=<?php echo $index;?>" onclick="return confirm('Are you sure?')" class="btn btn-danger btn-sm" style="width:50px;"><span class="glyphicon glyphicon-trash"></span></a>
                                    </div>
                                    </div>
                                </div>
                        </div>
                        <hr class="divider">
                        <?php
                        $index++;
                        }
                        ?>
                        </form>
                    <div class="panel-footer" style="height:9%;">
                    <div><a href="emptycart.php?action=empty" class="btn btn-danger" style="float:left;font-family:agency fb;">Empty Cart</a></div>
                    <div><a href="" data-toggle="modal" data-target=".bs-example-modal-sm" class="btn btn-success" style="float:right;font-family:agency fb;">Checkout</a></div>
                    <div style="float:right;margin-right:2%;font-family:agency fb;"><p class="form-control"><b>Total</b> Php: <?php echo $s;?></p></div>
                    </div>
                    </div>
                    </div>
                </div>
            </div>
                    <div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
                          <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                               <div class="modal-header" style="background-color:#cceaf6;">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="myModalLabel" style="font-family:agency fb;font-size:30px;">Costumer`s Information</h4>
                                  </div>
                                  <form method="POST" action="checkout.php">
                                  <div class="modal-body">
                                <div class="form-group">
                                <div class="row col-md-12">
                                    <div class="container inline col-md-4" >
                                            <input type="radio" class="form-control" name="order" onclick="showhidediv(this);" value="paypal">
                                            <img src="pic/paypal.jpg" style="width:200px;height:100px;">
                                    </div>
                                    <div class="col-md-4">
                                    <center><span style="font-family:agency fb; font-size: 50px;">or</span></center>
                                    </div>
                                    <div class="col-md-4">
                                            <input type="radio" class="form-control" name="order" onclick="showhidediv(this);" value="Cash On Delivery">
                                            <img src="pic/cash.png" style="width:200px;height:100px;">
                                    </div>
                                </div>
                                <div class="form-inline">
                                    <input type="text" class="form-control" name="lname" placeholder="Lastname" required>

                                    <input type="text" class="form-control" name="fname" placeholder="Firstname"required>

                                    <input type="text" class="form-control" name="mname" placeholder="Middlename"required>
                                </div>
                                <br>
                                    <input type="text" class="form-control" name="add" placeholder="Complete Address" style="width:596px;"required>
                                <br>
                                    <input type="email" class="form-control" name="email" placeholder="Email Address"style="width:596px;"required>
                                <br>                    
                                <input type="text" class="form-control" name="number" placeholder="Contact Number"style="width:196px;" required>
                                <br>
                                <div id="one" style="display:none;">
                                <div style="background-color:#cceaf6;"><p>Bill to another address. (Optional)</p></div>
                                    <div class="form-inline">

                                    <input type="text" class="form-control" name="lname2" placeholder="Lastname">

                                    <input type="text" class="form-control" name="fname2" placeholder="Firstname">

                                    <input type="text" class="form-control" name="mname2" placeholder="Middlename">
                                </div>
                                <br>
                                    <input type="text" class="form-control" name="add2" placeholder="Complete Address" style="width:596px;">
                                <br>
                                    <input type="email" class="form-control" name="email2" placeholder="Email Address"style="width:596px;">
                                <br>                    
                                <input type="text" class="form-control" name="number2" placeholder="Contact Number"style="width:196px;">
                                 </div>
                                 </div>
                            </div>
                            <div class="modal-footer">
                                    <button type="submit" name="confirm" class="btn btn-success" style="font-family:agency fb;font-size:15px;width:100px;">OK</button>
                                  </div>
                                  </form>
                          </div>
                        </div>
            </body>
            <script>
            function goBack() {
                window.history.back();
            }
            function showhidediv( rad )
                {
                    var rads = document.getElementsByName( rad.name );
                    document.getElementById( 'one' ).style.display = ( rads[1].checked ) ? 'block' : 'none';
                }
            </script>

错误

only 1 item stored.

1 个答案:

答案 0 :(得分:0)

您好,您需要更正此问题:'.$cart[$i]->image.'

for ($i=0; $i<count($cart); $i++) {
mysql_query("INSERT INTO order_details VALUES ('.$orderid.', '.$cart[$i]->name.', '.$cart[$i]->price.', '.$cart[$i]->quantity.', '.$cart[$i]->image.', '.$cart[$i]->price * $cart[$i]->quantity.','.$order.')");
}

另外,请在下次提出此类问题时突出显示代码中的错误行。