Is this code correct?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Is this code correct?

Post 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')");
                                }
                            }
 
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Is this code correct?

Post 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.
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Is this code correct?

Post 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???
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Is this code correct?

Post by aceconcepts »

What's $option_name1?
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Is this code correct?

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Is this code correct?

Post by aceconcepts »

Is player_id definitely a field in items? Seems unlikely.
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Is this code correct?

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Is this code correct?

Post 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')");
                                }
                            }
 
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Is this code correct?

Post 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?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Is this code correct?

Post by aceconcepts »

Take a look at the querie's WHERE clause 8O
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: Is this code correct?

Post 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
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Is this code correct?

Post 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.
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: Is this code correct?

Post 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.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: Is this code correct?

Post by aditya2071990 »

Oh, I are embarassed :)
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Is this code correct?

Post 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')");
                                }
                            }
}
 
Post Reply