我之前问过这样的问题,但现在我进行了更好的测试,并试图分析问题所在。这是ajax请求:
//start ajax request here//
$.post('purchaseitem.php',{itemAmount:itemAmount, itemId:itemId}, function(data){
$('.savestatus_'+itemId).text(data);
});
//end it here
我回显了 items 表中的所有项目,以及一个 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.";
}
}
以上是查询。/ ^ / 部分是失败的部分。当我截断表格,点击购买时,插入完全成功。但对于任何其他项目,插入失败。这是一个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