Mysql_query将数据插入" orders"下订单时的表格,但它没有将数据插入" order_detail"表。
它应该从Session' cart'中获取数据,但似乎失败了。
为了简单起见,我已经删除了一些HTML代码,只是离开了按钮,但这并不重要。
<?php
require_once('connect_to_db.php');
include("includes/functions.php");
session_start();
if ($_REQUEST['command'] == 'update') {
$customerid = mysql_insert_id();
$date = date('Y-m-d');
$result = mysql_query("insert into orders values('', '$date', '$customerid')");
$orderid = mysql_insert_id();
$max = count($_SESSION['cart']);
for ($i=0; $i<$max; $i++) {
$pid = $_SESSION['cart'][$i]['productid'];
$q = $_SESSION['cart'][$i]['qty'];
$price = get_price($pid);
mysql_query("insert into order_detail values ($orderid,$pid,$q,$price)");
}
die('Thank You! your order has been placed!');
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function validate(){
var f=document.form1;
f.command.value='update';
f.submit();
}
</script>
</head>
<body>
<form name="form1" onsubmit="return validate()">
<input type="hidden" name="command" />
<input type="submit" value="Place Order" />
</form>
</body>
</html>
以下是我将数据存储到Session&#39; cart&#39;
的方法function addtocart($pid,$q){
if($pid<1 or $q<1) return;
if(is_array($_SESSION['cart'])){
if(product_exists($pid)) return;
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['productid']=$pid;
$_SESSION['cart'][$max]['qty']=$q;
}
else{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['productid']=$pid;
$_SESSION['cart'][0]['qty']=$q;
}
}
答案 0 :(得分:-1)
尝试下面的代码,它可能会更好,但设置文件名并设置列名称。有
<?php
require_once('connect_to_db.php');
include("includes/functions.php");
session_start();
if (isset($_POST['update'])) {
$customerid = mysql_insert_id();
$date = date('Y-m-d');
$result = mysql_query("INSERT INTO orders (column1,column2,column3) VALUES('', '$date','$customerid')");
$orderid = mysql_insert_id();
$max = count($_SESSION['cart']);
for ($i=0; $i<$max; $i++) {
$pid = $_SESSION['cart'][$i]['productid'];
$q = $_SESSION['cart'][$i]['qty'];
$price = get_price($pid);
mysql_query("INSERT INTO order_detail (column1,column2,column3,column4) VALUES('$orderid','$pid','$q','$price')");
}
die('Thank You! your order has been placed!');
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<form name="form1" method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<input type="submit" name="update" value="Place Order" />
</form>
</body>
</html>