2012-04-19 15 views
0

私はこのようなことを以前に尋ねましたが、今はより良いテストを実行し、問題の内容を分析しようとしました。INSERT文は一度しか動作しません

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

私は、ユーザーが数量を入力し、購入をクリックできるようにするためにAHREFリンクおよび入力フィールドと一緒に項目のテーブル内のすべての項目を、アウトエコー: これは、Ajaxリクエストです。

<?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."; 
           } 
         } 

上記のクエリです。/^/partは失敗する部分です。 テーブルを切り捨てて[購入]をクリックすると、挿入は完全に成功します。しかし、他の項目の場合、挿入は失敗します。それは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

私はあなたがこれらの変数をサニタイズを願って... –

+0

私は、ユーザー入力変数のすべての並べ替えをサニタイズすることは非常にハードコードされたカスタム関数を持っています:) –

答えて

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ごとに1つの行しか持てません。私はあなたのアプリケーションを知らないが、それは間違っているようだ。

あなたは、テーブルを削除し、ゼロからスタート、その後、テーブルをドロップして再作成するには、使用することができた場合:

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カラムは、ユーザのセッション/クッキーIDとして挿入/評価されます bigint(20)、item_idは同じ、ユーザ名はvarchar、item_nameはvarchar、quantityはbigint(20)です。 自動インクリメントインデックスフィールドを追加しようとし、挿入を再試行します。 –

+0

質問を編集して、 'SHOW CREATE sector0_inventory'の出力を組み込み、それが役に立ちます。 –

+0

別のソリューションが含まれるように更新されました。 –

関連する問題