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!

 

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?

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!

 

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.

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");

 

 

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!

Archived

This topic is now archived and is closed to further replies.

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