Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:43:39+00:00 2026-06-02T06:43:39+00:00

I asked something like this earlier, but now I ran better tests, and tried

  • 0

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:

  //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.

<?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:

$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.
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.
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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:43:40+00:00Added an answer on June 2, 2026 at 6:43 am

    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:

    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.

    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. 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.

    Note that this will forfeit all of your data in that table…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize something like this has been asked, but this may be a little
Every now and again I get asked to install something like this on a
I know something like this has been asked before, but the answer was sort
Sorry If something like this has already been asked, but I can't find exactly
O.K. Maybe I have asked something similar to this, but this is another problem
A while ago I asked myself, when I write something like this: char* first(int
I saw something once for wrappers used to do this but it asked for
I know that this question might have been asked like 100 times, but, believe
I know that this question was asked earlier but the OP didn't get any
I had asked a question about this earlier, but it didn't get answered right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.