使用php在MYSQL数据库中存储订单详细信息

时间:2018-03-16 13:09:08

标签: php html mysql

我正在尝试通过开发电子商务网站来学习php和MYSQL。到目前为止,使用这些新语言的经验非常好,但我仍然坚持我的一个想法。我已设法将订单详细信息添加到我的数据库,但它一次只添加一个产品。最初我想要做的是添加更多产品并将其存储在数据库中。

举个例子,让我们说用户想买5件物品。如何将其存储到我的数据库中。使用我目前的代码,这只是作为单个产品添加,共有5个产品。(即使数量也没有增加)。

我不确定我的代码是否正确。对此有任何建议真的很感激。 (请随意修改代码,这将非常有帮助)

以下是我用于向数据库添加订单的代码。



<div class="col-md-12">
  <h4 class="text-center">Paypal Intergration Goes Here.</h4>
  <img src="./images/paywith_paypal.png" class="img-responsive  center-block" alt="Pay With Paypal">


  <?php
// Getting product details
$total = 0;
global $con;
$ip = getIp();

$sel_price = "SELECT * FROM zeus_limited.cart WHERE ip_address='$ip'";
$run_price = mysqli_query($con, $sel_price);

while($p_price=mysqli_fetch_array($run_price)){
  $product_id = $p_price['cart_product_id'];
  $product_price = "SELECT * FROM zeus_limited.product WHERE product_id='$product_id'";
  $run_product_price = mysqli_query($con,$product_price);

  while ($prod_price = mysqli_fetch_array($run_product_price)){
    $product_price = array($prod_price['product_price']);
    $product_id = $prod_price['product_id'];
    $pro_name = $prod_price['product_title'];
    $values = array_sum($product_price);
    $total +=$values;
  }
}

// Getting Quantity from cart
$get_quantity = "SELECT * FROM zeus_limited.cart WHERE cart_product_id='$product_id'";
$show_quantity = mysqli_query($con, $get_quantity);
$row_qty = mysqli_fetch_array($show_quantity);
$quantity = $row_qty['quantity'];
if($quantity == 0){
  $quantity = 1;
}
else {
  $quantity = $quantity;
  $total = $total * $quantity;
}

// Getting Customer details
$user = $_SESSION['customer_email'];
$get_customer = "SELECT * FROM zeus_limited.customer WHERE customer_email='$user'";
$show_customer = mysqli_query($con, $get_customer);
$row_c = mysqli_fetch_array($show_customer);

$customer_id = $row_c['customer_id'];
$customer_email = $row_c['customer_email'];
$customer_name = $row_c['customer_fname'];

$trx_id = mt_rand();
$currency = 'USD';
$invoice = mt_rand();


// Insert data to ORDER table
$add_order = "INSERT INTO zeus_limited.orders (order_product_id, order_customer_id, order_quantity, invoice_no, status, order_date) VALUES ('$product_id','$customer_id','$quantity','$invoice','in Progress',NOW())";
$run_order = mysqli_query($con, $add_order);

// Insert data to PAYMENT table
$add_payment = "INSERT INTO zeus_limited.payment (amount, payment_customer_id, payment_product_id, trx_id, payment_currency, payment_date) VALUES ('$total','$customer_id','$product_id','$trx_id','$currency',NOW())";
$run_payment = mysqli_query($con, $add_payment);

// Removing products from CART
$empty_cart = "DELETE FROM zeus_limited.cart";
$show_customerart = mysqli_query($con, $empty_cart);


if($total == $total){
  echo "<div class='text-center'>";
  echo "<h3>Welcome:" . $_SESSION['customer_email']. "<br>" . "Hooray! Your Payment was successful!</h3>";
  echo "<a href='./customer/my_account.php'>Go to your Account</a><br>";
  echo "</div>";

}
else {
  echo "<div class='text-center'>";
  echo "<h4>Welcome Guest, Payment process failed... Please try again</h4><br>";
  echo "<a href='/shop_products.php'>Go to Back to shop</a>";
  echo "</div>";
}

?>
</div>
&#13;
&#13;
&#13;

我也附上购物车代码。(如果需要)

&#13;
&#13;
<?php
session_start();
require_once './includes/init.php';
require_once './functions/functions.php';

echo getCart();

?>
<!DOCTYPE html>
<html>
<head>
  <title>Zeus Pvt. Ltd</title>
  <link rel="stylesheet" href="css/bootstrap.min.css" />
  <link rel="stylesheet" href="css/font-awesome.min.css" />
  <link rel="stylesheet" href="css/style.css" />
  <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
  <script src="js/jquery-3.2.1.min.js" type="text/javascript"></script>
  <script src="js/bootstrap.min.js"></script>
</head>
<body>
  <div class="header">
    <div class="container container-bg">
      <div class="row">
        <div class="col-md-4"><img src="images/logo.png" class="img-responsive" alt="Responsive image"></div>
        <div class="col-md-5"></div>
        <div class="col-md-3">
          <div class="zeus_cart">
            <div class="cart_bg">
              <ul class="cart">
                <i class="cart_icon"></i><p class="cart_desc"><?php getTotalCartPrice() ?><br><span class="yellow"><?php getTotalItems() ?></span></p>
                <div class='clearfix'></div>
              </ul>
              <ul class="product_control_buttons">
                <li><a href="#"><img src="images/close.png" alt=""/></a></li>
                <li><a href="#">Edit</a></li>
              </ul>
              <div class='clearfix'></div>
            </div>
            <ul class="quick_access">
              <li class="view_cart"><a href="cart.php">View Cart</a></li>
              <li class="check"><a href="checkout.php">Checkout</a></li>
              <div class='clearfix'></div>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="container container-bg">
      <nav class="navbar navbar-default">
        <div class="container">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand"></a>
          </div>
          <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
              <li><a href="index.php">Home</a></li>
              <li class="active"><a href="shop_products.php">Shop Products</a></li>
              <li><a href="customer/my_account.php">My Account</a></li>
              <li><a href="contact_us.php">Contact Us</a></li>
              <li>
                <?php
                if(!isset($_SESSION['customer_email'])){
                  echo '<a href="login.php">Login</a>';
                }
                else {
                  echo '<a href="logout.php">Logout</a>';
                }
                ?>
              </li>
              <li class="welcome"><a>Welcome <?php echo getUsername(); ?></a></li>
            </ul>
            <form method="get" action="results.php" enctype="multipart/form-data" class="navbar-form navbar-right">
              <div class="form-group search">
                <input type="text" name="user_query" class="form-control" placeholder="Search Products">
              </div>
              <button type="submit" class="btn btn-default btn-search"></button>
            </form>
          </div>
        </div>
      </div>
    </nav>
  </div>
  <div class="main">
    <div class="container container-bg">
      <div class="row">
        <div class="col-md-12">
          <div class="shoppingcart-title">
            <h4 class="text-center sidebar-main-menu">Shopping Cart</h4>
            <div class="table-responsive">
              <form action="" method="POST" enctype="multipart/form-data">
                <table class="table">
                  <thead>
                    <tr>
                      <th>Product No.</th>
                      <th>Product(s)</th>
                      <th>Name</th>
                      <th>Quantity</th>
                      <th>Unit Price</th>
                      <th>Sub Total</th>
                      <th>Remove</th>
                    </tr>
                  </thead>
                </tbody>
                <?php
                global $con;

                $total = 0;
                $ip = getIp();
                $select_price = "SELECT * FROM zeus_limited.cart WHERE zeus_limited.cart.ip_address='$ip'";
                $run_price = mysqli_query($con, $select_price);

                while ($product_price = mysqli_fetch_array($run_price)) {

                  $product_id = $product_price['cart_product_id'];
                  $product_quantity = $product_price['quantity'];
                  $product_price = "SELECT * FROM zeus_limited.product WHERE zeus_limited.product.product_id ='$product_id'";
                  $run_product_price = mysqli_query($con, $product_price);

                  while ($product_new_price = mysqli_fetch_array($run_product_price)) {

                    $product_price = array($product_new_price['product_price']);
                    $product_id = $product_new_price['product_id'];
                    $product_image = $product_new_price['product_image_carousel'];
                    $product_title = $product_new_price['product_title'];
                    $unit_product_price = $product_new_price['product_price'];

                    $values = array_sum($product_price);
                    $total += $values * $product_quantity;

                    ?>

                    <tr>
                      <td><?php echo $product_id; ?></td>
                      <td><img src="images/products/<?php echo $product_image; ?>" </td>
                      <td><?php echo $product_title; ?></td>
                      <td><input id="" type="text" name="quantity[<?php echo $product_id; ?>]" size="5" value="<?php echo $product_quantity; ?>" style="text-align:center;"/>
                        <input type="hidden" name="product_id[<?php echo $product_id; ?>]" value="<?php echo $product_id; ?>"
                      </td>

                      <!--Updating the quantity-->
                      <?php
                      $ip = getIp();

                      if (isset($_POST['update_cart'])){

                        foreach ($_POST['product_id'] as $pid => $id) {

                          $product_id = $id;
                          $product_quantity = $_POST['quantity'][$pid];
                          $update_products = "UPDATE zeus_limited.cart SET quantity = '$product_quantity' WHERE cart_product_id = '$product_id' AND ip_address = '$ip';";
                          $run_update = mysqli_query($con, $update_products);
                        }
                        if($update_products){

                          echo "<script>window.open('cart.php','_self')</script>";
                        }
                      }
                      ?>

                      <td><?php echo 'Rs '. $unit_product_price; ?></td>
                      <td><?php echo 'Rs '.$unit_product_price * $product_quantity  ?></td>
                      <td><input type="checkbox" name="remove[]" value="<?php echo $product_id; ?>"/></td>
                    </tr>
                  </tbody>
                  <?php } } ?>
                  <thead>
                    <tr>
                      <th></th>
                      <th></th>
                      <th></th>
                      <th></th>
                      <th>Grand Total: </th>
                      <th><?php echo 'Rs '. $total ?></th>
                      <th></th>
                    </tr>
                  </thead>
                </table>
                <div class="cart_buttons">
                  <input type="submit" name="update_cart" value="Update Cart"/>
                  <input type="submit" name="continue_shopping" value="Continue Shopping"/>
                  <input type="submit" name="checkout" value="Checkout"/>
                  <?php if (isset($_POST['checkout'])){echo "<script>window.open('checkout.php','_self')</script>"; } ?>
                </div>
              </form>
              <?php
              global $con;
              $ip = getIp();

              if (isset($_POST['update_cart'])){
                foreach ($_POST['remove'] as $remove_id) {

                  $delete_product = "DELETE FROM zeus_limited.cart WHERE cart_product_id='$remove_id' AND ip_address='$ip'";
                  $run_delete = mysqli_query($con, $delete_product);

                  if($run_delete){

                    echo "<script>window.open('cart.php','_self')</script>";
                  }
                }
              }

              if (isset($_POST['continue_shopping'])){

                echo "<script>window.open('shop_products.php','_self')</script>";
              }
            ?>
          </div>
        </div>
      </div>
      <div class="clearfix"></div>
    </div>
  </div>
</div>
</div>
<div class="footer">
  <div class="container">
    <div class="row">
      <div class="col-md-4 footer-grid">
        <h3>Menu</h3>
        <ul class="list1">
          <li><a href="index.php">Home</a></li>
          <li><a href="shop_product.php">Shop Products</a></li>
          <li><a href="customer/my_account.php">My Account</a></li>
          <li><a href="contact_us.php">Contact Us</a></li>
          <li><a href="login.php">Login</a></li>
        </ul>
      </div>
      <div class="col-md-4 footer-grid">
        <h3>Your Account</h3>
        <ul class="list1">
          <li><a href="#">My Orders</a></li>
          <li><a href="#">Edit Account</a></li>
          <li><a href="#">Change Password</a></li>
          <li><a href="#">Delete Account</a></li>
          <li><a href="#">My Cart</a></li>
        </ul>
      </div>
      <div class="col-md-4 footer-grid">
        <h3>About Us</h3>
        <p class="footer_desc">Zeus is a pharmacy focused on providing patients with what they need and deserve - exceptional pharmacy care. It is our responsibility and passion to care for your medication needs.</p>
        <p class="f_text"><span class="fa fa-phone" aria-hidden="true"></span>  Phone:  &nbsp;&nbsp;&nbsp;+081 123 45 67</p>
        <p class="email"><span class="fa fa-envelope" aria-hidden="true"></span>  Email: &nbsp;&nbsp;&nbsp;<span>info@zeuspharmacy.com</span></p>
      </div>
      <div class="clearfix"> </div>
    </div>
  </div>
</div>
</div>
<div class="footer-bottom">
  <div class="container">
    <div class="row">
      <div class="col-md-12 footer-grid-bottom">
        <div class="copyrights">
          <p>&copy; 2017 Zeus Pharmacy. Made with <span class="fa fa-heart" aria-hidden="true"></span> by <a href="https://dilumtharaka.github.io">Dilum Tharaka</a></p>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>
&#13;
&#13;
&#13;

order table structure image

payment table structure image

以上是我使用的桌面结构。提前致谢

0 个答案:

没有答案