Page 1 of 2
Is this code correct?
Posted: Wed Oct 08, 2008 10:09 pm
by cdoyle
Hi,
I've been working on this all night, and getting close but just can't quite get it right.
I'm trying to create a function that will first look at the items table, and then see if the player already has the item. If he does, it will perform the update query. If he doesn't it will perform the insert query.
It was working fine when I only had 1 record in the table, once I added more then one. it doesn't seem to do what I think it should. It inserts even tho a player already has the item.
I think the problem is my $itemcheck and the array. I don't think I wrote that right, but not sure.
Code: Select all
$itemcheck = mysql_query("SELECT * FROM items");
while($row = mysql_fetch_array($itemcheck))
{
if ( $item_number == "1")
{
$itemtype = 10;
if ($row['player_id'] == $option_name1 && $row['item_id'] == $itemtype)
{
$updateweapon = mysql_query("UPDATE items SET quantity = quantity + '15' Where player_id = '$option_name1' and item_id = '$itemtype'");
}
else
{
$insertweapon = mysql_query("INSERT into items(player_id,item_id,status)values('$option_name1','$itemtype','unequipped')");
}
}
Re: Is this code correct?
Posted: Thu Oct 09, 2008 2:30 am
by papa
First of all you can echo out this row and see what it says:
Code: Select all
if ($row['player_id'] == $option_name1 && $row['item_id'] == $itemtype)
See if really $option_name1 matches etc.
Re: Is this code correct?
Posted: Thu Oct 09, 2008 10:07 am
by cdoyle
If I have only 1 record in the table it works.
If $option_name matches it does what it's suppose too,
If it doesn't match it also does what it's suppose too.
but if there are more then 1 record, then everything stops working.
it's like it's not checking the entire table, but just the last record inserted???
Re: Is this code correct?
Posted: Thu Oct 09, 2008 10:19 am
by aceconcepts
What's $option_name1?
Re: Is this code correct?
Posted: Thu Oct 09, 2008 10:40 am
by cdoyle
$option_name1 = $_POST['option_name1']; << $POST is sent from paypal.
This is all part of my paypal IPN script, in the email that the script send me the variable matches the ID of the player. So I believe it's OK.
This is a hard script to bug because it all runs in the background, there is nothing to actually see on the page.
Re: Is this code correct?
Posted: Thu Oct 09, 2008 10:46 am
by aceconcepts
Is player_id definitely a field in items? Seems unlikely.
Re: Is this code correct?
Posted: Thu Oct 09, 2008 10:49 am
by cdoyle
yes there is a players_ID in the items table. This table is used for the inventory of each player.
items table
id
player_id
item_id
status
quantity
Re: Is this code correct?
Posted: Thu Oct 09, 2008 10:59 am
by aceconcepts
You could simply take the player_id out of the condition like this:
Code: Select all
$itemcheck = mysql_query("SELECT * FROM items WHERE player_id='$option_name1'");
while($row = mysql_fetch_array($itemcheck))
{
if ( $item_number == "1")
{
$itemtype = 10;
if ($row['item_id'] == $itemtype)
{
$updateweapon = mysql_query("UPDATE items SET quantity = quantity + '15' Where player_id = '$option_name1' and item_id = '$itemtype'");
}
else
{
$insertweapon = mysql_query("INSERT into items(player_id,item_id,status)values('$option_name1','$itemtype','unequipped')");
}
}
Re: Is this code correct?
Posted: Thu Oct 09, 2008 11:21 am
by cdoyle
I think it needs to be there,
It needs to check to make sure the player doesn't already have the item they just bought.
If they do, the script needs to add to the quantity.
If they don't then the script needs to add a new row.
If we take the player_ID out of the condition, how will it know if the player already has it?
Re: Is this code correct?
Posted: Thu Oct 09, 2008 11:24 am
by aceconcepts
Take a look at the querie's WHERE clause

Re: Is this code correct?
Posted: Thu Oct 09, 2008 11:32 am
by aditya2071990
Hello there,
If this is part of a paypal IPN script, then I would suggest that you not break your head over coding it...there is an excellent tool here:
https://www.paypaltech.com/SG2/
All you have to do is to simply give a few details, like the database name, location and login [you can give dummy values and edit them later if you are paranoid], and it outputs a wonderful chunk of code, that will take care of the complex paypal post-back process. This script writes all the details like payer-name, txn-id, payment_status and other stuff into a table in your mysql database (they also give the mysql statements to make that table, look in the help section).
Now you can use all that data in the table however you wish.
The only additional task you have to do is give the file a name (filename.php for example), upload it to your server and in your paypal profile (I mean the seller's), you have to set the post-back url to 'filename.php', sit back and enjoy as all your troubles are solved.
I tried to code that script for 2 weeks to no avail, and then I saw this recommended by paypal themselves, and now I am using it successfully on my site.
All the best,
Aditya
Re: Is this code correct?
Posted: Thu Oct 09, 2008 11:40 am
by cdoyle
didn't see that there
Just gave it a try, now it will update the field if that player already has the weapon.
But if the player doesn't have the weapon, and not listed in the table yet. My insert query doesn't insert them now.
Re: Is this code correct?
Posted: Thu Oct 09, 2008 11:41 am
by cdoyle
aditya2071990 wrote:Hello there,
If this is part of a paypal IPN script, then I would suggest that you not break your head over coding it...there is an excellent tool here:
https://www.paypaltech.com/SG2/
All you have to do is to simply give a few details, like the database name, location and login [you can give dummy values and edit them later if you are paranoid], and it outputs a wonderful chunk of code, that will take care of the complex paypal post-back process. This script writes all the details like payer-name, txn-id, payment_status and other stuff into a table in your mysql database (they also give the mysql statements to make that table, look in the help section).
Now you can use all that data in the table however you wish.
The only additional task you have to do is give the file a name (filename.php for example), upload it to your server and in your paypal profile (I mean the seller's), you have to set the post-back url to 'filename.php', sit back and enjoy as all your troubles are solved.
I tried to code that script for 2 weeks to no avail, and then I saw this recommended by paypal themselves, and now I am using it successfully on my site.
All the best,
Aditya
This is actually the code I'm using.
I just need it to update my game tables too.
Re: Is this code correct?
Posted: Thu Oct 09, 2008 12:28 pm
by aditya2071990
Oh, I are embarassed

Re: Is this code correct?
Posted: Thu Oct 09, 2008 12:52 pm
by aceconcepts
In that case then:
Code: Select all
$itemcheck = mysql_query("SELECT * FROM items WHERE player_id='$option_name1'");
if(mysql_num_rows($itemcheck)==0)
{
//INSERT PLAYER OR WEAPON ETC...
}
else
{
while($row = mysql_fetch_array($itemcheck))
{
if ( $item_number == "1")
{
$itemtype = 10;
if ($row['item_id'] == $itemtype)
{
$updateweapon = mysql_query("UPDATE items SET quantity = quantity + '15' Where player_id = '$option_name1' and item_id = '$itemtype'");
}
else
{
$insertweapon = mysql_query("INSERT into items(player_id,item_id,status)values('$option_name1','$itemtype','unequipped')");
}
}
}