简体   繁体   English

INSERT语句只能运行一次

[英]INSERT statement works only once

I asked something like this earlier, but now I ran better tests, and tried to analyze what the problem was. 我之前问了这样的问题,但现在我运行了更好的测试,并试图分析问题所在。 This is the ajax request: 这是ajax请求:

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

I echo out all the items in the items table, along with an ahref link and an input field to allow the user to type in the quantity and click buy. 我回显了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
    }
?>

This is a test version of my code, so I know that it is messed up... The php part: 这是我的代码的测试版本,所以我知道它搞砸了...... 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.";
                                }
                        }

The above is the query. 以上是查询。 the / ^ / part is the part that fails. / ^ / part是失败的部分。 When I Truncate the table, and click buy, the insertion is completely successful. 当我截断表格并单击“购买”时,插入完全成功。 But for any other item, insertion fails. 但对于任何其他项目,插入失败。 It's a php, I guess, I am really confused. 这是一个PHP,我想,我真的很困惑。 relative table to the insert and update queries 插入和更新查询的相对表

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

Show the output of CREATE TABLE sector0_item to get more help, but my guess is that your primary key on that table is id and you're trying to specify that manually in your INSERT statement: 显示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')

Your primary key must be unique for each row. 您的主键对于每一行必须是唯一的。 Try: 尝试:

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

That will work if your id column is set to AUTO INCREMENT . 如果您的id列设置为AUTO INCREMENT那将会有效。

EDIT: After you posted the table structure, your problem is the database table design. 编辑:发布表结构后,您的问题是数据库表设计。 Right now the primary key is id which means you can only have one row per PHP session ID. 现在主键是id ,这意味着每个PHP会话ID只能有一行。 I don't know your application, but that seems wrong. 我不知道你的申请,但这似乎不对。

If you can delete the table and start from scratch, then DROP the table and re-create it using: 如果您可以删除该表并从头开始,则删除该表并使用以下命令重新创建它:

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

then restore your PHP back to the way you had it. 然后将PHP恢复到原来的状态。

Note that this will forfeit all of your data in that table... 请注意,这将放弃该表中的所有数据...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM