Jump to content
MakeWebGames

Item Selecting


Nicholas

Recommended Posts

Hello Everyone, I'm having troubles selecting 1 specific item so the user can use it but the link won't show if they don't have the item in their inventory :)

Item: Jail Card

Item ID: 23

item database table

_________________________________________________________________________________________

| ItemID | ItemName | ItemDesc | ItemBuyPrice | ItemSellPrice | Item Effects | Weapon Power | Armour Power |

|__23___|_Jail Card_|__ N/A___|____1000___|_____0_____|_No Jail Time_|______0______|______0______|

inventory database table

_______________________________

| UserID | InvID | InvItemID | InvQty |

|___1__|_1523_|___23____|__56___|

Right thats the information about the tables and item i want to select. So basically what im doing is when your in Jail youll get a message saying

Your in jail for another .... minutes. (Use Jail Card)

this is what the code bit is like:

 

if($user['JailTime'] != 0) {
echo "Your in jail for another ".abs(intval($user['JailTime']))." minutes. ";
$jail = mysql_query("SELECT i.ItemID, inv.UserID, inv.InvID, inv.InvItemID, inv.InvQty FROM inventory inv LEFT JOIN items i ON inv.InvItemID=23 WHERE inv.InvID=".abs(intval($_GET['ID']))." AND inv.UserID=".abs(intval($user['UserID']))."");
$jailcard = mysql_fetch_array($jail);
if($jailcard['InvQty'] > 0) {
echo "(<a href='ItemUse.php?ID=".abs(intval($_GET['ID']))."'>Use Jail Card</a>)";
}
}

 

does anyone have any ideas how i can make it so it actually selects that specific item if they own it and so it selects the "InvID" bit for the "ItemUse File"?

if this is even understandable on what i mean? :) if you can help me that be great :D

Link to comment
Share on other sites

I do believe the echo statement should be changed from this
echo "(<a href='ItemUse.php?ID=".abs(intval($_GET['ID']))."'>Use Jail Card</a>)";

to this

echo "<a href='ItemUse.php?ID={$jailcard['InvItemID']}'>Use Jail Card</a>";

note: not tested - just a rough guess by your description. This doesn't seem the be standard, so can't really look at things myself.

the problem im having i think is the link isnt showing up and it wont select the $_GET['ID'] which is the InvID value? so maybe the SELECT query in wrong?

also what do you mean "this doesnt seem to be standard"? :s

Link to comment
Share on other sites

right i got this far...

if($user['JailTime'] != 0) {
echo "Your in jail for another ".abs(intval($user['JailTime']))." minutes. ";

$grab = mysql_query("SELECT UserID, InvID, InvItemID FROM inventory WHERE InvItemID=23 AND UserID=".abs(intval($user['UserID']))."");
$code = mysql_fetch_array($grab);

$jail = mysql_query("SELECT i.ItemID, inv.UserID, inv.InvID, inv.InvItemID, inv.InvQty FROM inventory inv LEFT JOIN items i ON inv.InvItemID=i.ItemID WHERE inv.InvID=".abs(intval($code['InvID']))." AND inv.inv_userid=".abs(intval($user['UserID']))."");
$jailcard = mysql_fetch_array($jail);

if($jailcard['InvQty'] > 0) {
echo "(<a href='ItemUse.php?ID=".abs(intval($jailcard['InvID']))."'>Use Jail Card</a>)";
}

}

it shows the link, sometimes works but sometimes doesnt select the correct InvID so it gives an Invalid ID error... Any ideas on how to fix this? :)

Link to comment
Share on other sites

Yeah sorry about that, ignore my previous post. Apparently itemuse.php requires the inv_id value and not item_id value as I would have expected, seems rather strange honestly but that's the way it is. I don't see what's going wrong with the code here then. The $_GET['id'] has the value of the inv_id though, correct? Or what does it hold on the page?

With the "this does not seem standard" I meant that it's not a part of the original mcc v2 script, right?

Link to comment
Share on other sites

Yeah sorry about that, ignore my previous post. Apparently itemuse.php requires the inv_id value and not item_id value as I would have expected, seems rather strange honestly but that's the way it is. I don't see what's going wrong with the code here then. The $_GET['id'] has the value of the inv_id though, correct? Or what does it hold on the page?

With the "this does not seem standard" I meant that it's not a part of the original mcc v2 script, right?

i used another query to try and select the $_GET['ID'] for the query under it which you can see and it kinda worked.. just sometimes not selecting the correct Inventory ID sometimes and cant figure out why...

also yes your correct, its not mccodes v2 :) its my own engine/coding style. i just didnt know where else to post for help lol.

its still kinda the same in a way from SELECTING and etc thought..

Link to comment
Share on other sites

Too many questions remain unanswered, so I am going to list a few.

1. You are trying to join using an item id, which can not work. To join, you must link 2 table values. (inv.InvItemID=23 should be a where clause)

2. Your items table has no id row? If so, you need one to add to the link. ($_GET['ID'] just does not cut it. Your items table must be functioning somehow and we need to know how it calls the data to be used)

3. Why are you using abs(intval($var)) when I assume that the field it is reading from is int() (Therefore a numeric value anyway)

1) i did that with one my other posts but that one has 2 queries on it. (POST: #4)

2) InvID is the id row for the inventory. just UserID is inserted first, doesnt affect anything.

3) im not sure, i just try to use some seems of securing method to numbers being selected or inserted into the database, so it doesnt mess up and not become a Whole Number. But yes it is a INT numeric anyways :)

Edited by Nicholas
Link to comment
Share on other sites

actually do you need all that information? From what I see now, all data you need, and are in fact using, can be found in the inventory table. Which means you could just use (correct me if I'm wrong here):

$sql = "SELECT * FROM inventory WHERE UserID '{$user['userID']}' AND InvItemID = 23";

From this query you can take the InvID to be used with the itemuse.php page.

Link to comment
Share on other sites

$sql = "SELECT `i.ItemName` as `item_name`, `inv.InvID` as `item_id` 
       FROM `items` i LEFT JOIN `inventory` inv 
       ON `i.ItemID` = `inv.InvItemID` 
       WHERE `inv.UserID` = '{$user['UserID']}' AND `inv.InvItemID` = 23 LIMIT 1";
if (($result = mysql_query($sql)) == true) {
   echo '<a href="ItemUse.php?ID='.$result['item_id'].'">Use Item: '.htmlspecialchars($result['item_name']).'</a>';
}

doesnt work? doesnt show the ID of the item just does this:

ItemUse.php?ID=

no number at the end; and it doesnt show the Item Name neither.

 

$sql = "SELECT * FROM inventory WHERE UserID='{$user['UserID']}' AND InvItemID = 23";
if (($result = mysql_query($sql)) == true) {
   echo '<a href="ItemUse.php?ID='.$result['InvID'].'">Use Item: Jail Card</a>';
}

 

that doesnt work neither, doesnt show the ID= ........... bit :s

Edited by Nicholas
Link to comment
Share on other sites

Any errors?

A few tips.

1. Sometimes PHP does not produce an error, when phpmyadmin would.

2. $sql is pre-defined for one reason (for me at least) - debugging.

before the $result line write:

echo $sql;

You guessed it - that displays the query and from there, copy and paste it into PHPmyAdmin and let me know if there are any errors.

right im not sure if i did it how you said but i did this...

$sql = "SELECT * FROM inventory WHERE UserID='{$user['UserID']}' AND InvItemID = 23";
echo $sql;

and on the game file it showed this...

$sql = "SELECT * FROM inventory WHERE UserID='1' AND InvItemID = 23";

i copied that, and ran it on PHPmyAdmin SQL and it didnt give me a error. it shows this...

_______________________________

| UserID | InvID | InvItemID | InvQty |

|___1__|_4148_|___23____|__344__|

Link to comment
Share on other sites

mysql_query returns a resource id, it doesn't return an array filled with the details, so you must use mysql_fetch_array too.

And yeah SomeRandomBastard, I agree with your method actually, I was just trying to point out that it could be done that way and make life easier here. After all, the itemid is hardcoded as well.

@nicholas try this

$sql = "SELECT * FROM inventory WHERE UserID='{$user['UserID']}' AND InvItemID = 23";
if (($result = mysql_query($sql)) == true) {
   $result = mysql_fetch_array($result);
   echo '<a href="ItemUse.php?ID='.$result['InvID'].'">Use Item: Jail Card</a>';
}
Link to comment
Share on other sites

mysql_query returns a resource id, it doesn't return an array filled with the details, so you must use mysql_fetch_array too.

And yeah SomeRandomBastard, I agree with your method actually, I was just trying to point out that it could be done that way and make life easier here. After all, the itemid is hardcoded as well.

@nicholas try this

$sql = "SELECT * FROM inventory WHERE UserID='{$user['UserID']}' AND InvItemID = 23";
if (($result = mysql_query($sql)) == true) {
   $result = mysql_fetch_array($result);
   echo '<a href="ItemUse.php?ID='.$result['InvID'].'">Use Item: Jail Card</a>';
}

this one works :) thanks. still get the odd Invalid Item ID message but it works fine :)

thanks for the help guys (Y)

Edited by Nicholas
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...