我希望这种解释有意义......
我有一个使用SESSION / PHP / MySQLi工作的购物车,我的理由是我无法通过Checkout屏幕根据登录的客户ID显示ShipTo地址。
数据库表是“客户”,字段名称是“id”
工作:
客户可以注册/登录
页面受非成员保护
所有购物车详细信息都会通过结帐
不工作:(checkout.php)
根据ID显示当前客户ShipTo - 当前设置为默认ID“5”,以便我可以测试它。
的login.php 这将在登录前检查email / psw是否存在并使用相同的“sesscustomerID”引用。
<?php
session_start();
include("db.php"); //Establishing connection with database
$error = ""; //Variable for storing our errors.
if(isset($_POST["submit"]))
{
if(empty($_POST["email"]) || empty($_POST["password"]))
{
$error = "Both fields are required.";
}else
{
// Define $email and $password
$email=$_POST['email'];
$password=$_POST['password'];
// To protect from MySQL injection
$email = stripslashes($email);
$password = stripslashes($password);
$email = mysqli_real_escape_string($db, $email);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
//Check email and password from database
$sql="SELECT id FROM customers WHERE email='$email' and password='$password'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
//If email and password exist in our database then create a session.
//Otherwise echo error.
if(mysqli_num_rows($result) == 1)
{
$_SESSION['sessCustomerID'] = $email; // Initializing Session
header("location: products.php"); // Redirecting To Other Page
}else
{
$error = "Incorrect email or password.";
}
}
}
?>
checkout.php
<?php
include 'check.php';
?>
<?php
// include database configuration file
include 'dbConfig.php';
// initializ shopping cart class
include 'Cart.php';
$cart = new Cart;
// redirect to home if cart is empty
if($cart->total_items() <= 0){
header("Location: products.php");
}
// set customer ID in session
$_SESSION['sessCustomerID'] = 5;
// get customer details by session customer ID
$query = $db->query("SELECT * FROM customers WHERE id = ".$_SESSION['sessCustomerID']);
$custRow = $query->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Checkout</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.container{width: 100%;padding: 50px;}
.table{width: 65%;float: left;}
.shipAddr{width: 30%;float: left;margin-left: 30px;}
.footBtn{width: 95%;float: left;}
.orderBtn {float: right;}
</style>
</head>
<body>
<h1 class="hello">Hello, <em><?php echo $login_user;?>!</em></h1>
<a href="logout.php" style="font-size:18px">Logout?</a>
<div class="container">
<h1>Order Preview</h1>
<table class="table">
<thead>
<tr>
<th>Scent</th>
<th>Type</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<?php
if($cart->total_items() > 0){
//get cart items from session
$cartItems = $cart->contents();
foreach($cartItems as $item){
?>
<tr>
<td><?php echo $item["name"]; ?></td>
<td><?php echo $item["category"]; ?></td>
<td><?php echo '£'.$item["price"].' GBP'; ?></td>
<td><?php echo $item["qty"]; ?></td>
<td><?php echo '£'.$item["subtotal"].' GBP'; ?></td>
</tr>
<?php } }else{ ?>
<tr><td colspan="4"><p>No items in your cart......</p></td>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td colspan="3"></td>
<td class="text-right"><strong>Total</strong></td>
<?php if($cart->total_items() > 0){ ?>
<td class="text-left"><strong><?php echo '£'.$cart->total().' GBP'; ?></strong></td>
<?php } ?>
</tr>
<tr>
<td><a href="products.php" class="btn btn-warning"><i class="glyphicon glyphicon-menu-left"></i> Continue Shopping</a></td>
<td colspan="3"></td>
<td><a href="cartAction.php?action=placeOrder" class="btn btn-success orderBtn">Place Order <i class="glyphicon glyphicon-menu-right"></i></a></td>
</tr>
</tfoot>
</table>
<div class="shipAddr">
<h4>Shipping Details</h4>
<p><?php echo $custRow['name']; ?></p>
<p><?php echo $custRow['email']; ?></p>
<p><?php echo $custRow['phone']; ?></p>
<p><?php echo $custRow['address']; ?></p>
</div>
</div>
</body>
</html>