Author Topic: ** MySQL UPDATE and WHERE clause help! **  (Read 185 times)

0 Members and 1 Guest are viewing this topic.

Offline bford21Topic starter

  • Irregular
    • View Profile
** MySQL UPDATE and WHERE clause help! **
« on: February 08, 2010, 09:29:00 PM »
Hey guys! I am new to the forums and am looking for some help. First off I am using 000webhost.com as a host and it is running PHP    5.2. and MySQL 5.0.81-community.

I am fairly new to PHP and completely new to MySQL. I am self taught and normally can figure things out myself but I am really puzzled on this one.

I am in the process of creating a text based game. The problem comes when I have the user go to "the mines" and "mine" for gold. Right now I have it set so every time they mine they get 100 gold. I want the users gold to go up by 100 obviously and I want it reflected in both my database and the members profile.

I assume to do this I would use the MySQL UPDATE function. This is what I have been doing but I cant seem to figure out how to get only that users gold to go up. I did some research and found out about the WHERE clause and have tried to use it to identify the user but I cant seem to work it out.

This is what I have so far:

I connect to the the MySQL server, and select the database then I wrote this line of code:

Code: [Select]
$temp_gold = $_SESSION['MEMBER_GOLD'] + 100;


$result = mysql_query("UPDATE members SET gold= $temp_gold WHERE member_id = ’{$_SESSION['MEMBER_ID']}'");

$_SESSION['MEMBER_GOLD'] is the members gold when the user first logged in and $_SESSION['MEMBER_ID'] is the users ID.

To me the statement makes perfect sense. Update the table "members" and set "gold" equal to "$temp_gold" which is the gold before + 100. Obviously this is wrong though and I would really appreciate your guys help! Thanks in advance.

« Last Edit: February 08, 2010, 09:31:38 PM by bford21 »

Online thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
    • View Profile
Re: ** MySQL UPDATE and WHERE clause help! **
« Reply #1 on: February 09, 2010, 01:39:33 AM »
Are you getting any error? You have a syntax error in your statement. Notice the strange ' char ?


$result 
mysql_query("UPDATE members SET gold= $temp_gold WHERE member_id = '{$_SESSION['MEMBER_ID']}'");

Offline Mchl

  • Guru
  • Freak!
  • *
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • BOINC@Poland
Re: ** MySQL UPDATE and WHERE clause help! **
« Reply #2 on: February 09, 2010, 03:04:51 AM »
And you can actually do
$result mysql_query("UPDATE members SET gold= gold+100 WHERE member_id = '{$_SESSION['MEMBER_ID']}'");
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Code: [Select]
brugere i panik

Offline bford21Topic starter

  • Irregular
    • View Profile
Re: ** MySQL UPDATE and WHERE clause help! **
« Reply #3 on: February 09, 2010, 09:49:35 PM »
Hey guys thanks for your help. The problem ended up being that strange ' char. I think I copied and pasted that code from somewhere so I didn't write it myself.

Sorry about now being so descriptive. I'm sure I'll have more questions to ask in the upcoming days. Thanks!

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.