[英]Session and mysql_query doesn't insert data to table
下訂單時,Mysql_query將數據插入“ orders”表中,但不會將數據插入“ order_detail”表中。
它應該從會話“購物車”中獲取數據,但是似乎失敗了。
為了簡單起見,我帶走了一些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>
這是我將數據存儲到會話“購物車”的方式
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;
}
}
嘗試下面的代碼,它可能會更好,但是可以設置文件名並設置列名。 那里
<?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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.