2012-04-19 91 views
0

我早些時候問過類似的問題,但現在我運行了更好的測試,並試圖分析問題所在。 這是Ajax請求:INSERT語句只能工作一次

//start ajax request here// 
    $.post('purchaseitem.php',{itemAmount:itemAmount, itemId:itemId}, function(data){ 
    $('.savestatus_'+itemId).text(data); 
    }); 
    //end it here 

我回聲出在項目表中的所有項目,與AHREF鏈路和輸入字段以及允許用戶在數量類型,然後點擊購買。

<?php 
    $shop_query = mysql_query("SELECT * FROM sector0_item WHERE item_location = '$chapter'"); 
    while(($shop_row = mysql_fetch_array($shop_query))){ 
     $itemid = $shop_row['item_id']; 
     $item_name = $shop_row['item_name']; 
     $item_price = $shop_row['item_price']; 
     ?> 
     <div class = "item_name"><?php echo $item_name; ?></div> 
     <div class = "item_price"><?php echo $item_price; ?></div> 
     <input type = 'text' class = "purchaseAmount_<?php echo $itemid;?>" name = "purchaseAmount" /> 
     <a id = "purchaseButton_<?php echo $itemid; ?>" href = "prevent default();" class = "purchase_button" onclick = "buy(); return false;">Buy</a> 
     <div class = 'savestatus_<?php echo $itemid; ?>'></div> 
     <hr /><br /> 
     <?php 
    } 
?> 

這是我的代碼的測試版本,所以我知道它是搞砸... PHP的一部分:

$user_inventory_query = mysql_query("SELECT * FROM sector0_inventory WHERE id = '$dbid' AND item_id = '$item_id'"); 
         $checking_item_inventory = mysql_num_rows($user_inventory_query); 
         if($checking_item_inventory === 0){ 
          /*^*/ $insertion_into_inventory = mysql_query("INSERT INTO `sector0_inventory`(`id`, `item_id`, `username`, `item_name`, `quantity`) VALUES ('$dbid','$item_id','$dbuser','$item_name','$purchase_amount')"); 
           if($insertion_into_inventory === true){ 
           mysql_query("UPDATE sector0_players SET cash = cash-'$total_cost' WHERE id = '$dbid'"); 
           echo "Purchase complete"; 
           } 
         }else if ($checking_item_inventory === 1){ 
          $update_inventory_quantities = mysql_query("UPDATE sector0_inventory SET quantity = quantity+'$purchase_amount' WHERE id = '$dbid' AND item_id = '$item_id'"); 
          if($update_inventory_quantities===true) { 
           mysql_query("UPDATE sector0_players SET cash = cash-'$total_cost' WHERE id = '$dbid'"); 
           echo "Purchase complete, quantity updated."; 
           } 
         } 

以上是查詢。 /零件是失敗的零件。 當我截斷表並點擊buy時,插入完全成功。但對於任何其他項目,插入失敗。這是一個PHP,我想我真的很困惑。 相表的插入和更新查詢

CREATE TABLE `sector0_inventory` (
`id` bigint(20) NOT NULL COMMENT 'The input in this field will be php code exclusive. No increment allowed.', 
`item_id` bigint(20) NOT NULL COMMENT 'The input is also code exclusive', 
`username` varchar(250) NOT NULL COMMENT 'This value will be used to search for the user inventory information. Admin privileges only', 
`item_name` varchar(250) NOT NULL COMMENT 'This value will be used to identify (user side) the item. It will be used by admins to query out when a removal of a specific item is needed', 
`quantity` bigint(20) NOT NULL COMMENT 'This value will be 0 by default BIG int is to allow calculations', 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 
+0

我希望你消毒這些變量... – 2012-04-19 23:35:17

+0

我有一個非常硬編碼的自定義函數來淨化每個類型的用戶輸入變量:) – 2012-04-19 23:38:00

回答

2

顯示的CREATE TABLE sector0_item輸出,以獲得更多的幫助,但我的猜測是,你在該表的主鍵是id和你想手動指定,在您INSERT語句:

INSERT INTO `sector0_inventory`(`id`, `item_id`, `username`, `item_name`, `quantity`) VALUES ('$dbid','$item_id','$dbuser','$item_name','$purchase_amount') 

您的主鍵對於每一行必須是唯一的。嘗試:

INSERT INTO `sector0_inventory`(`item_id`, `username`, `item_name`, `quantity`) VALUES ('$item_id','$dbuser','$item_name','$purchase_amount') 

如果您id列設置爲AUTO INCREMENT,將工作。

編輯:在您發佈表結構後,您的問題是數據庫表設計。現在主鍵是id,這意味着每個PHP會話ID只能有一行。我不知道你的申請,但這似乎是錯誤的。

如果你能刪除表,並從頭開始,然後刪除該表,並使用重新創建它:

CREATE TABLE `sector0_inventory` (
`transaction_key` INT NOT NULL AUTO INCREMENT COMMENT 'The unique ID for each row', 
`id` bigint(20) NOT NULL COMMENT 'The input in this field will be php code exclusive.  No increment allowed.', 
`item_id` bigint(20) NOT NULL COMMENT 'The input is also code exclusive', 
`username` varchar(250) NOT NULL COMMENT 'This value will be used to search for the  user inventory information. Admin privileges only', 
`item_name` varchar(250) NOT NULL COMMENT 'This value will be used to identify (user  side) the item. It will be used by admins to query out when a removal of a   specific item is needed', 
`quantity` bigint(20) NOT NULL COMMENT 'This value will be 0 by default BIG int is to  allow calculations', 
PRIMARY KEY (`transaction_key`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

然後恢復你的PHP還給你有它的方式。

注意,這將失去所有的數據在表...

+0

第一個id列插入/作爲用戶的會話/ cookie ID 它是一個bigint(20),item_id是相同的,用戶名是varchar,item_name是varchar,數量是bigint(20)。 我會嘗試添加一個自動增量索引字段,然後重試插入。 – 2012-04-19 23:36:32

+0

編輯問題以包含'SHOW CREATE sector0_inventory'的輸出,這將有所幫助。 – 2012-04-19 23:39:49

+0

已更新,以包含其他解決方案。 – 2012-04-19 23:47:45