dongyi7669 2016-02-28 06:55
浏览 59
已采纳

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

checkout.php ............... I use this code it. When i buy 2 or more items, only 1 item stored in my database. thanks in advance...................

<?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();
}
?>

help me please i`m a newbie :( 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>

Error

only 1 item stored.
  • 写回答

1条回答 默认 最新

  • dragon8899 2016-02-28 07:03
    关注

    Hi you need to correct this: '.$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.')");
    }
    

    Also please highlight the error line in the code next time you ask such question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号