Php INSERT语句只工作一次

Php INSERT语句只工作一次,php,mysql,insertion,Php,Mysql,Insertion,我之前问过类似的问题,但现在我运行了更好的测试,并试图分析问题所在。 这是ajax请求: //start ajax request here// $.post('purchaseitem.php',{itemAmount:itemAmount, itemId:itemId}, function(data){ $('.savestatus_'+itemId).text(data); }); //end it here 我回显items表中的所有项目,以及一个ahref

我之前问过类似的问题,但现在我运行了更好的测试,并试图分析问题所在。 这是ajax请求:

  //start ajax request here//
   $.post('purchaseitem.php',{itemAmount:itemAmount, itemId:itemId}, function(data){
    $('.savestatus_'+itemId).text(data);
   });
  //end it here
我回显items表中的所有项目,以及一个ahref链接和一个输入字段,允许用户键入数量并单击buy

<?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
    }
?>
以上是查询。/^/部分是失败的部分。 当我截断表并单击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

显示
CREATE TABLE sector0\u 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恢复到原来的状态


请注意,这将丢失该表中的所有数据…

我希望您清理这些变量…我有一个非常硬编码的自定义函数来清理每种类型的用户输入变量:)第一个id列作为用户的会话/cookie id插入/赋值,它是一个bigint(20),item_id相同,username是一个varchar,项目名称为varchar,数量为bigint(20)。我将尝试添加一个自动增量索引字段,然后重试插入。编辑问题以包含
SHOW CREATE sector0\u inventory
的输出,这将有所帮助。已更新以包含另一个解决方案。这确实有效!谢谢你的回答,也谢谢你的解释,非常感谢!
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