我尝试使用insert语句将会话中的数据插入数据库。我从登录屏幕传递数据,该屏幕存储在名为“login_user”的会话变量中。我还有一个名为“books”的会话,它存储多个变量,例如书籍的ISBN号,标题和价格。
我有以下代码:
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View shopping cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css"></head>
<body>
<br>
<div id="books-wrapper">
<!-- #content to center the menu -->
<div id="content">
<!-- This is the actual menu -->
<ul id="darkmenu">
<li><a href="home.php">Home</a></li>
<li><a href="catalogue.php">Catalogue</a></li>
<li><a href="search.php">Search</a></li>
<li><a href= "view_cart.php">Cart</a></li>
<li><a href="#">Orders</a></li>
</ul>
<div id = "welcome" >
Welcome, <?=$_SESSION['login_user']?>! <br> <a href="logout.php">Logout</a>
</div>
</div>
<br><br>
<h1 id = "mainHeader" >View Cart</h1>
<br>
<div class="view-cart">
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION["books"]))
{
if(isset($_POST['submit_btn']) ){
$sql = "INSERT INTO `orders` (`OrderNo`, `BookName`, `Quantity`, `TotalPrice`, `ISBN`, `StudentID`) VALUES (NULL, '$obj->Title', '$cart_itm['quantity']', '$total', '$ISBN', '$_SESSION['login_user']');";
}else {
$total = 0;
echo '<form method="post" action="">';
echo '<ul>';
$cart_items = 0;
foreach ($_SESSION["books"] as $cart_itm)
{
$ISBN = $cart_itm["ISBN"];
$results = $mysqli->query("SELECT Title,BookDesc,Price FROM books WHERE ISBN='$ISBN'");
$obj = $results->fetch_object();
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["ISBN"].'&return_url='.$current_url.'">×</a></span>';
echo '<div class="p-Price">'.$currency.$obj->Price.'</div>';
echo '<div class="book-info">';
echo '<h3>'.$obj->Title.' (ISBN :'.$ISBN.')</h3> ';
echo '<div class="p-quantity">Quantity : '.$cart_itm["quantity"].'</div>';
echo '<div>'.$obj->BookDesc.'</div>';
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm["Price"]*$cart_itm["quantity"]);
$total = ($total + $subtotal);
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->Title.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$ISBN.'" />';
echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->BookDesc.'" />';
echo '<input type="hidden" name="item_quantity['.$cart_items.']" value="'.$cart_itm["quantity"].'" />';
$cart_items ++;
}
echo '</ul>';
echo '<span class="check-out-txt">';
echo '<strong>Total : '.$currency.$total.'</strong> ';
echo '</span>';
echo '<button name="sumbit_btn" class="save_order">Save Order</button>';
echo '</form>';
}else{
echo 'Your Cart is empty';
}
}
?>
</div>
</div>
</body>
</html>
但是我的插入语句似乎不起作用。
我在第43行得到了( ! ) Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
,这是插入语句
我有一个名为Catalog的页面,其中变量被实例化,但这些变量被传递到上面的视图购物车页面。
这个陈述有什么问题吗?
答案 0 :(得分:0)
用以下代码替换43行
$sql = "INSERT INTO `orders` (`OrderNo`, `BookName`, `Quantity`, `TotalPrice`, `ISBN`, `StudentID`) VALUES (NULL, $obj->Title, $cart_itm['quantity'], $total, $ISBN, $_SESSION['login_user']);";
让我知道它的工作与否?。
答案 1 :(得分:0)
用
代替第43行$sql = "INSERT INTO `orders` (`OrderNo`, `BookName`, `Quantity`, `TotalPrice`, `ISBN`, `StudentID`) VALUES (NULL, '{$obj->Title}', '{$cart_itm['quantity']}', '{$total}', '{$ISBN}', '{$_SESSION['login_user']}');";
记得清理变量!