2012-04-19 3 views
0

이전에 이런 질문을했지만 이제는 더 나은 테스트를 실행하고 문제가 무엇인지 분석하려고했습니다.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."; 
           } 
         } 

위의 쿼리입니다./^/부분은 실패한 부분입니다. 테이블을 자르고 구매를 클릭하면 삽입이 성공적으로 완료됩니다. 그러나 다른 항목의 경우 삽입이 실패합니다. 그것은 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') 

기본 키는 각 행마다 고유해야합니다. 보십시오 : 당신의 id 열이 AUTO INCREMENT로 설정되어있는 경우

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

가 작동합니다.

편집 : 테이블 구조 게시 후 데이터베이스 테이블 디자인 문제입니다. 현재 기본 키는 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 열은 사용자의 세션/쿠키 ID로 삽입/평가됩니다. bigint (20)이고 item_id는 동일하며 username은 varchar이고 item_name은 varchar이고 quantity는 bigint (20)입니다. 자동 증가 인덱스 필드를 추가하려고 시도하고 삽입을 다시 시도합니다. –

+0

'SHOW CREATE sector0_inventory'의 결과를 포함하도록 질문을 편집하면 도움이 될 것입니다. –

+0

다른 솔루션을 포함하도록 업데이트되었습니다. –

관련 문제