Jump to content

Question about changing certain data in MySQL database


echoindia756

Recommended Posts

Hi there,

 

I'm designing an online game website. I'm having a little trouble getting something to work right. You see players of the game can buy "credits", these allow them to buy things to further their rank etc and make them stronger than other players. But the problem i'm having is that when a player buys credits, it gives that player credits, but it also gives every other player in the database credits! So if one person buys 100 credits, every user in the database gets 100 credits.

This is the code i'm using:

 

mysql_query("UPDATE `game_planets` SET `belcredits`=`belcredits`+'100'");

 

How would I change that code so it only updates the person's credits who actually bought them? I was thinking of something like getting the username that the user used to login, or maybe somebody can think of a better way to solve this. Thanks in advance!

 

Link to comment
Share on other sites

your update query will update all the records in your belcredits because you dont have any condition "where clause" that says what record to be updated

 

I tried a few Where Clause's, but what clause would I write so that it only updated the users credits that actually bought the credits?

Link to comment
Share on other sites

Yea, that would be the logical thing - but how do I get the user id, I mean, I know you use the SELECT command to get things but how do I tell the code to identify the current user and match it up with the data in the username's field? If I could do that, that would solve the problem!

 

Link to comment
Share on other sites

mysql_query("UPDATE `game_planets` SET `belcredits`=`belcredits`+100 WHERE `username`='username' LIMIT 1");

 

There is an example of how it would update only one record based on username being usersname

 

though I highly suggest using an ID field and ID being a UNIQUE.

Link to comment
Share on other sites

mysql_query("UPDATE `game_planets` SET `belcredits`=`belcredits`+100 WHERE `username`='username' LIMIT 1");

 

There is an example of how it would update only one record based on username being usersname

 

though I highly suggest using an ID field and ID being a UNIQUE.

 

Thanks! I have an ID Field in the database called id_owner and another field with the name of the planet. So would this work?

 

mysql_query("UPDATE `game_planets` SET `belcredits`=`belcredits`+100 WHERE `name`='id_owner' LIMIT 1");

 

 

Link to comment
Share on other sites

You know how you said:

 

it would be WHERE `id_owner`='[however you have your data set]'

 

in the "however you have your data set", what exactly goes there? Another table? e.g

`game_planets.id_owner`='game_planets.name' LIMIT 1");

                  This is puzzling me!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.