無法寫入資料庫

edited 十月 2013 in PHP新手區
請問能幫我看看這段程式是否有問題?裏面的資料無法寫入資料庫,會有錯誤訊息,但是只要拿掉”product_name”,就沒問題,也就是這中文名字這資料寫不進去.(資料庫中有列”product_name”這個欄位)
找不到問題在哪,還請知道的人幫一下忙,拜託,謝謝~

<?php
while($row_RecCart = mysql_fetch_assoc($RecCart)){
$order_id = $_SESSION;
$product_name = $row_RecCart;
$product_id = $row_RecCart;
$product_quantity = $row_RecCart;
$product_price = $row_RecCart;

$linetotal = $product_quantity * $product_price;

$insertSQL = "INSERT INTO orderdetail(order_id,product_id,";
$insertSQL .= "product_quantity,product_price,linetotal,product_name)";
$insertSQL .= "VALUES ($order_id,$product_id,$product_quantity,";
$insertSQL .= "$product_price,$linetotal,$product_name)";

mysql_query($insertSQL, $shopp) or die(mysql_error());
}
header("Location: customer.php");
?>

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=4337

評論

  • edited 八月 2009
    錯誤訊息是什麼
  • edited 八月 2009
    錯誤訊息:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '???90,190)' at line 1
  • edited 八月 2009
    來源的”cart”資料表中也有"product_name"這個欄位,而且有資料.
    以下是全部的程式碼:

    <?php require_once("cart.inc.php"); ?>
    <?php require_once('Connections/shopp.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }

    $colname_RecCart = "-1";
    if (isset($_SESSION)) {
    $colname_RecCart = $_SESSION;
    }
    mysql_select_db($database_shopp, $shopp);
    $query_RecCart = sprintf("SELECT * FROM cart WHERE uniquekey = %s ORDER BY product_id ASC", GetSQLValueString($colname_RecCart, "text"));
    $RecCart = mysql_query($query_RecCart, $shopp) or die(mysql_error());
    //$row_RecCart = mysql_fetch_assoc($RecCart);
    $totalRows_RecCart = mysql_num_rows($RecCart);
    ?>
    <?php
    while($row_RecCart = mysql_fetch_assoc($RecCart)){
    $order_id = $_SESSION;
    $product_name = $row_RecCart;
    $product_id = $row_RecCart;
    $product_quantity = $row_RecCart;
    $product_price = $row_RecCart;

    $linetotal = $product_quantity * $product_price;

    $insertSQL = "INSERT INTO orderdetail(order_id,product_id,";
    $insertSQL .= "product_quantity,product_price,linetotal)";
    $insertSQL .= "VALUES ($order_id,$product_id,$product_quantity,";
    $insertSQL .= "$product_price,$linetotal)";

    mysql_query($insertSQL, $shopp) or die(mysql_error());
    }
    header("Location: customer.php");
    ?>

    mysql_free_result($RecCart);
    ?>
  • edited 八月 2009
    錯在這行
    $insertSQL .= "VALUES ($order_id,$product_id,$product_quantity,";
    因為$product_quantity是字串資料,根據SQL規範,必需用單引號或雙引號包圍
    $insertSQL .= "VALUES ($order_id,$product_id,'{$product_quantity}',";
    另外多筆insert用迴圈執行mysql_query()不是很好
    MySQL允許下面這種做法
    INSERT INTO 資料表名稱 (欄位名稱)VALUSE(插入值1),(插入值2),.....
    這樣就只需用一次mysql_query()
  • edited 八月 2009
    馬上試試,謝謝 ^^
Sign In or Register to comment.