簡體   English   中英

會話和mysql_query不在表中插入數據

[英]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;
        }
    }
  1. 在HTML中,每個表單都應具有方法(“ GET”或“ POST”)或操作(文件),以防您使用相同的文件(在這種情況下),只需將文件名放在此處即可。
  2. 在這種情況下,在PHP中,您不應該使用請求,而應該使用post或get,因為請求都可以處理,使用post或get可能會更快一些。
  3. 您可以在不使用javascript的情況下優化html文件,而無需使用此javascript。
  4. 您應該避免使用mysql_query代替PDO(數據對象)。
  5. 我猜想orderid和productid是相同的,您應該只使用一個。
  6. 您應該在第二個查詢中添加“”。
  7. 您應該將表中的列命名為:“ INSERT INTO table_name(column1,column2,column3,...)VALUES(value1,value2,value3,...)”,檢查添加的值是否不超過您的值mysqlDb中的表可以處理。

嘗試下面的代碼,它可能會更好,但是可以設置文件名並設置列名。 那里

<?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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM