2014-12-03 109 views
0

這是一個簡單的購物車,我發現某處,很久以前,現在需要做一個訂購系統。但是由於某些或其他奇怪的原因,我不能插入所有的字段!插入MySQL不能正常工作

我下面的功能:

function get_categoryid($pid){ 
$result=mysql_query("select product_category_id from shopping_products where serial=$pid") or die("select product_category_id from shopping_products where serial=$pid"."<br/><br/>".mysql_error()); 
$row=mysql_fetch_array($result); 
return $row['product_category_id']; 
} 
function get_thecategory($pid){ 
$result=mysql_query("select product_category_title from shopping_products where serial=$pid") or die("select product_category_title from shopping_products where serial=$pid"."<br/><br/>".mysql_error()); 
$row=mysql_fetch_array($result); 
return $row['product_category_title']; 
} 
} 

這裏是它被插入到「shopping_order_detail」表中的客戶機填補了他/她的信息之後的PHP代碼。

if (isset($_REQUEST['command']) && $_REQUEST['command']=='update'){ 
    $name=$_REQUEST['name']; 
    $email=$_REQUEST['email']; 
    $address=$_REQUEST['address']; 
    $phone=$_REQUEST['phone']; 
    $loyalty=$_REQUEST['loyalty']; 

    $result=mysql_query("insert into shopping_customers values('','$name','$email','$address','$phone','$loyalty')"); 
    $customerid=mysql_insert_id(); 
    $date=date('Y-m-d'); 
    $result=mysql_query("insert into shopping_orders values('','$date','$customerid')"); 
    $orderid=mysql_insert_id(); 

    /* ---- below my problem i think --- */ 
    $max=count($_SESSION['cart']); 
    for($i=0;$i<$max;$i++){ 
     $pid=$_SESSION['cart'][$i]['productid']; 
     $q=$_SESSION['cart'][$i]['qty']; 
     $price=get_price($pid); 
     $pcategoryid=get_categoryid($pid); 
     $pcategory=get_thecategory($pid); 
     $pvendor=get_thevendor($pid); 

     mysql_query("INSERT INTO shopping_order_detail VALUES($orderid,$pid,$q,$price,$pcategoryid,$pcategory,$pvendor)"); 

    echo "vendor -" . $pvendor . " Order ID - " . $orderid . " - Product ID - " . $pid . " - Category ID ". $pcategoryid ." - Price R ". $price .".00 - Quantity ". $q. "- Category Title: ". $pcategory ."<Br>"; 
    } 
    echo "<br>=======================================================================================<br>"; 
    echo "<br>Data echoed above this works fine.. and pulls the correct shopping items<br>"; 
    echo "<br>Sucess ......Data Inserted!<br>"; 
    die('Thank You! your order has been placed'); 
} 

這是我的問題!

將「$ pcategoryid」插入到表格中,但是在此之後我所添加的任何字段數量不僅無法插入,而且由於某些原因也會阻止任何信息被插入到這段腳本中。

回顧一下我的問題: 如果從腳本中刪除「$ pcategory」和「$ pvendor」,數據將被插入。如果我在問題中添加這兩個字段,那麼腳本不會插入,但是/並且它不會給出任何錯誤消息。

我的「回聲===等」在腳本的底部命令讓我發現,我的腳本工作,因爲它顯示了問題的兩個領域時,它循環通過產品清單

我已經把我的PHP錯誤代碼是這樣的:

ini_set('display_errors',1); 
ini_set('display_startup_errors',1); 
error_reporting(-1); 

這是Mysql中的某種限制,因爲表中的問題沒有主鍵?我無法解決問題!

請任何幫助將不勝感激謝謝。

+0

在旁註中,請使用mysql和NON參數化查詢請求STOP。 – Jordy 2014-12-03 15:26:38

+0

你可以發佈查詢的回聲嗎? 'echo「INSERT INTO shopping_order_detail VALUES($ orderid,$ pid,$ q,$ price,$ pcategoryid,$ pcategory,$ pvendor)」' – Giwwel 2014-12-03 15:27:17

+0

請確保您的插入查詢中的字段順序是正確的,並且這些字段實際上存在於你的桌子上。在旁註中,請停止使用mysql和非參數化查詢。你會在以後節省一些嚴重的頭痛。 – sotoz 2014-12-03 15:30:00

回答

1

試試這個

$結果= mysql_query( 「插入shopping_customers VALUES( '$名稱', '$電子郵件', '$地址', '$電話', '$忠誠')」);

而且

$result=mysql_query("insert into shopping_orders values('$date','$customerid')"); 
+0

謝謝@Ranjeet Singh!在你和以前的用戶Giwwel之間我找到了解決方案 – Cavemanharris 2014-12-03 15:52:11

1

get_thecategory($pid)結果是類別標題,一個字符串.. 所以$pcategory必須加引號的查詢是這樣的:

mysql_query("INSERT INTO shopping_order_detail VALUES($orderid,$pid,$q,$price,$pcategoryid,'$pcategory',$pvendor)"); 

可能是多個變量字符串。在查詢中也引用這些內容

+0

謝謝@Giwwel!我是PHP新手,現在才意識到所有其他字段都不是字符串。謝謝。有用 ! – Cavemanharris 2014-12-03 15:50:48

+0

我很高興我能幫上忙 – Giwwel 2014-12-03 15:57:14