Just Ace Posted June 21, 2015 Posted June 21, 2015 Very simple and slightly silly question i'm fairly new to using sql's, an example of what I mean below: $db->query("UPDATE users set money=money+{$money} WHERE userid={$userid}"); What areas of that code do i need to change in order for it to be putting an item into someones inventory. (Example I want it to be Item ID 5) Quote
KyleMassacre Posted June 21, 2015 Posted June 21, 2015 Very simple and slightly silly question i'm fairly new to using sql's, an example of what I mean below: $db->query("UPDATE users set money=money+{$money} WHERE userid={$userid}"); What areas of that code do i need to change in order for it to be putting an item into someones inventory. (Example I want it to be Item ID 5) Wait, what???? How can you not do this ^^^ but you say this?: So after very nearly recoding an MCCodes engine into practically my very own, I don't feel like just a Browser release is good enough these days, so I've been thinking of some other options, which you'll probably all agree, is how most games and apps become successful nowadays. I mean seriously, this is some basic stuff especially if you did what you claimed to do and you would know that it is much easier to do than what you posted as an example line of code. Quote
Just Ace Posted June 21, 2015 Author Posted June 21, 2015 Don't embarrass me too much :P I intend to pay for a lot of things and it's never too late to learn. Quote
KyleMassacre Posted June 21, 2015 Posted June 21, 2015 Don't embarrass me too much :P I intend to pay for a lot of things and it's never too late to learn. Well you did say you rewrote MCC to make it your own. What exactly did you rewrite? Apparently it wasn't enough to make it your own otherwise you would have come across the item_add() function or at least knew what the function was. This is is not about the money or paying for anything. I'll do it for free: item_add($userid,$r['item_number'],1); I just find it remarkable that you claim one thing but can't perform a simple insert query without the use of the function if it didn't exist. You don't have to lie to kick it here and I know you have been around for a while now. Don't worry I won't put you on blast until I need to. Quote
Jax Posted June 21, 2015 Posted June 21, 2015 Alternatively, you could look at the problem from the point of view of what actually needs to be done. Consider the implementation of the inventory table: inv_id : The unique ID of this inventory (user/item pair) (automatically generated) inv_itemid : The item ID itself inv_userid : The user ID (owner of said item) inv_qty : The quantity owned Now to add to this we can do the following: /** * Add an item to a user's inventory * * $user <integer> (or numeric string) The ID of the user to give an item to * $item <integer> (or numeric string) The ID of the item to add * $qty <integer> (or numeric string) The quantity of $item to give to $user **/ function give($user, $item, $qty=1) { global $db; // Update the existing inventory row $sql = <<<SQL UPDATE `inventory` SET `inv_qty` = `inv_qty` + $qty WHERE `inv_itemid` = $item AND `inv_userid` = $user SQL; $db->query($sql); // If nothing was changed, add a new row if (!$db->affected_rows()) { $sql = <<<SQL INSERT INTO `inventory` (`inv_itemid`, `inv_userid`, `inv_qty`) VALUES ($item, $user, $qty) SQL; $db->query($sql); } } Purists may point out a potential race condition however this cannot be circumvented cleanly without altering the database itself. However, it does perform more efficiently than the builtin item_add function which suffers from both race conditions and potential data loss. The key to figuring out Mccodes is to learn how the database is structured; it's not elegant I'll admit, but it is a beginning. With a little care, you can easily make it into something really special. Please note, the above function is intentionally not safe from SQL injection -- I assume that however, most if not all people are perfectly capable if ensuring that only integers or at least strings passing ctype_digit() as passed to it. Quote
NonStopCoding Posted June 21, 2015 Posted June 21, 2015 to add a item via mccodes use the item add function item_add(usersid,itemid,amount); Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.