查询即时使用它将一些数据插入一个名为“purchase”的表中我有一个会话变量,其中包含当前登录名为“login_user”的人的用户名,其他所有数据都在实例化中上一页:
这是我目前的代码:
<?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"]))
{
$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="submit_btn" class="save_order">Save Order</button>';
echo '</form>';
if(isset($_POST['submit_btn']) ){
$sql = "INSERT INTO `purchases` (`OrderNo`,`BookName`,`Quantity`,`TotalPrice`,`ISBN`,`StudentID`) VALUES (NULL, '" . $obj->Title . "', '" . $cart_itm['quantity'] . "', '" . $total . "', '" . $ISBN . "', '" . $_SESSION['login_user'] . "');";
}
}
?>
</div>
</div>
</body>
</html>
我遇到的问题是当用户点击“保存订单”按钮时没有任何反应。没有数据插入数据库。
我已经回显了变量以检查它们是否包含我想要的数据并且看起来有效,但插入语句似乎没有做任何事情
有什么想法吗?
答案 0 :(得分:0)
表单的结尾只有一个按钮,脚本不知道你希望它是提交按钮。
更改
echo '<button name="submit_btn" class="save_order">Save Order</button>';
到
echo '<input type="submit" name="submit_btn" value="Save Order" />';
另外,当您提交时,您只需定义$sql
字符串,您需要$mysqli_query
。
答案 1 :(得分:0)
您需要执行查询:
$sql = "INSERT INTO `purchases` (`OrderNo`,`BookName`,`Quantity`,`TotalPrice`,`ISBN`,`StudentID`) VALUES (NULL, '" . $obj->Title . "', '" . $cart_itm['quantity'] . "', '" . $total . "', '" . $ISBN . "', '" . $_SESSION['login_user'] . "');";
$stmt = $mysqli->prepare($sql)
$stmt->execute();
答案 2 :(得分:-2)
echo '<form method="post" action="">';
操作无效,必须有一个像“/index.php”
的网址