2012-04-19 90 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