Whats wrong?

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

Linjon
Forum Newbie
Posts: 8
Joined: Fri Feb 12, 2010 11:18 am

Whats wrong?

Post by Linjon »

Hello. Im stuck and need some help. If i add points by my scripti then its give points only for first user in database and change id to 0 (zero). Here is the code:

Code: Select all

<?php
  mysql_connect("localhost", "*******", "*****");
  mysql_select_db("*********");
  $result = mysql_query("SELECT `player_id` FROM `wc3_player` WHERE `player_name` ='".$_GET['message']."'");
  if(mysql_num_rows($result) == 0) {
  echo 'Cant find that name';
  }
  if(mysql_num_rows($result) > 0) {
  echo 'Points are added';
  $addxp = $row['race_xp'] = 75000; 
  mysql_query("UPDATE `wc3_race` SET `player_id`='".$_GET['message']."', `race_id` ='1', `race_xp`= `race_xp` + ".$addxp);
  }
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Whats wrong?

Post by social_experiment »

If you wish to add data (or update) to a specific record you have to use the 'WHERE' syntax in your mySQL query, like you did in your first query

Code: Select all

<?php $result = mysql_query("SELECT `player_id` FROM `wc3_player` WHERE `player_name` ='".$_GET['message']."'"); ?>
You will then have something along the lines of :

Code: Select all

<?php mysql_query("UPDATE `wc3_race` SET `player_id`='".$_GET['message']."', `race_id` ='1', `race_xp`= `race_xp` + ".$addxp WHERE player_name = '".$_GET['message']."'); ?>
In your first query you are setting the player_id to the value of $_GET['message'], is that what you are refering to when you say :
and change id to 0 (zero).
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Whats wrong?

Post by josh »

Why would you name a field something_id and then store a string in it? You probably meant to type a different column name. Strings cast to integers usually end up as 0 in many languages.
Linjon
Forum Newbie
Posts: 8
Joined: Fri Feb 12, 2010 11:18 am

Re: Whats wrong?

Post by Linjon »

Here is full code:

Code: Select all

 mysql_connect("localhost", "*****", "*****");
mysql_select_db("*********");
  $result = mysql_query("SELECT `player_id` FROM `wc3_player` WHERE `player_name` ='".$_GET['message']."'"); 
  if(mysql_num_rows($result) == 0) {
  echo 'Cant find that user!';
  }
  if(mysql_num_rows($result) > 0) { 
  echo 'Points added.'; 
  $addxp = $row['race_xp'] = 75000
  mysql_query("UPDATE `wc3_player_race` SET `player_id`='".$_GET['message']."', `race_id` ='1', `race_xp`= `race_xp` + ".$addxp);
  }
?>
Maybe someone dont understand but if i add points with that script then user who is first in database and have id "1" then after adding points user have id "0", thats problem. Mysql database for this game is quite difficult and i cant change it.
User avatar
a.heresey
Forum Commoner
Posts: 59
Joined: Wed Dec 13, 2006 7:31 pm
Location: Chesapeake, VA, US

Re: Whats wrong?

Post by a.heresey »

You're not specifying which record to update using your $result.

Code: Select all

 
mysql_connect("localhost", "*****", "*****");
mysql_select_db("*********");
 
//escape get data and make sure it's there before you use it
$message=(isset($_GET['mesage']))?mysql_real_escape_string($_GET['message']):'';
$result = mysql_query("SELECT `player_id` FROM `wc3_player` WHERE `player_name` ='".$message."'");
  
if(mysql_num_rows($result) != 1) {
echo 'Cant find that user!';
}else{
$addxp = $row['race_xp'] = 75000;
//add a WHERE clause and don't SET the player_id value
mysql_query("UPDATE `wc3_player_race` SET `race_id` ='1', `race_xp`= `race_xp` + ".$addxp."WHERE `player_id` =".$result['player_id']);
echo 'Points added.';  
}
 
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Whats wrong?

Post by social_experiment »

Maybe someone dont understand but if i add points with that script then user who is first in database and have id "1" then after adding points user have id "0", thats problem.
What 'id' are you refering to? The primary key id field? The player_id field (is this perhaps your primary key field?)? From the code you have pasted it cannot be determined where 'id' is set to 0.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Linjon
Forum Newbie
Posts: 8
Joined: Fri Feb 12, 2010 11:18 am

Re: Whats wrong?

Post by Linjon »

I have these three tables in database:

Code: Select all

CREATE TABLE IF NOT EXISTS `wc3_player_race` (
  `player_id` int(8) unsigned NOT NULL DEFAULT '0',
  `race_id` tinyint(4) unsigned NOT NULL DEFAULT '0',
  `race_xp` int(8) DEFAULT NULL,
  PRIMARY KEY (`player_id`,`race_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
--
-- Table `wc3_player_race`
--
 
INSERT INTO `wc3_player_race` (`player_id`, `race_id`, `race_xp`) VALUES
(3, 4, 215),

Code: Select all

CREATE TABLE IF NOT EXISTS `wc3_player` (
  `player_id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `player_steamid` varchar(25) NOT NULL DEFAULT '',
  `player_ip` varchar(20) NOT NULL DEFAULT '',
  `player_name` varchar(35) NOT NULL DEFAULT '',
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`player_id`),
  KEY `player_name` (`player_name`),
  KEY `player_ip` (`player_ip`),
  KEY `player_steamid` (`player_steamid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5430;
 
--
-- Table `wc3_player`
--
 
INSERT INTO `wc3_player` (`player_id`, `player_steamid`, `player_ip`, `player_name`, `time`) VALUES
(3, '', '', 'UserName', '2010-10-24 15:11:20');

Code: Select all

CREATE TABLE IF NOT EXISTS `wc3_config` (
 
`config_id` varchar(50) NOT NULL,
 
`config_value` varchar(255) NOT NULL,
 
PRIMARY KEY (`config_id`)
 
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
--
 
-- Table `wc3_config`
 
--
 
INSERT INTO `wc3_config` (`config_id`, `config_value`) VALUES
 
('level0_xp', '0'),
 
('level1_xp', '400'),
 
('level2_xp', '800'),
 
('level3_xp', '1600'),
 
('level4_xp', '3200'),
 
('level5_xp', '6400'),
 
('level6_xp', '12800'),
 
('level7_xp', '25600'),
 
('level8_xp', '51200'),
 
('level9_xp', '102400'),
 
('level10_xp', '204800'),
 
('version', '3.0 RC13'),
 
('sql_conversion', '1');
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Whats wrong?

Post by social_experiment »

Code: Select all

 
 CREATE TABLE IF NOT EXISTS `wc3_player_race` (
   `player_id` int(8) unsigned NOT NULL DEFAULT '0',
   `race_id` tinyint(4) unsigned NOT NULL DEFAULT '0',
   `race_xp` int(8) DEFAULT NULL,
   PRIMARY KEY (`player_id`,`race_id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 

Code: Select all

<?php mysql_query("UPDATE `wc3_player_race` SET `player_id`='".$_GET['message']."', `race_id` ='1', `race_xp`= `race_xp` + ".$addxp); ?>
You set 'player_id' as a primary key and then in your query (above) you set 'player_id' to '$_GET['message']', if $_GET['message'] is blank, i think that will set the value of 'player_id' to 0. Is 'player_id' supposed to be a field that is only set once and serve as a unique identifier for each record, and if so, why do you want it updated?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Whats wrong?

Post by josh »

social_experiment wrote: You set 'player_id' as a primary key and then in your query (above) you set 'player_id' to '$_GET['message']', if $_GET['message'] is blank, i think that will set the value of 'player_id' to 0. Is 'player_id' supposed to be a field that is only set once and serve as a unique identifier for each record, and if so, why do you want it updated?
Yeah I already asked him and his response was "Mysql database for this game is quite difficult and i cant change it." :roll: Him posting his schema confirms that is his problem.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Whats wrong?

Post by social_experiment »

He also said :
...but if i add points with that script then user who is first in database and have id "1" then after adding points user have id "0"
at which my reply was aimed. I didn't infere any changes were to be made. Merely an observation on why the query reacts as it does.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Whats wrong?

Post by josh »

Was rolling my eyes at him not you
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Whats wrong?

Post by social_experiment »

Ah. Apologies for my misdirected animosity.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Linjon
Forum Newbie
Posts: 8
Joined: Fri Feb 12, 2010 11:18 am

Re: Whats wrong?

Post by Linjon »

So .. i have looked this script and now i understand what i did wrong. Should i REPLACE function? Should i take user and points from table and then add some points and then replace these?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Whats wrong?

Post by josh »

Stop trying to re-assign a field you asked the database to assign for you (the player_id is an auto increment). This is the 3rd time someone has explained the problem to you ;-)
Linjon
Forum Newbie
Posts: 8
Joined: Fri Feb 12, 2010 11:18 am

Re: Whats wrong?

Post by Linjon »

I tried everything what you told but still nothing :(
Here is my code:
<?php
mysql_connect("localhost", "********", "******");
mysql_select_db("********");
$result = mysql_query("SELECT `player_id` FROM `wc3_player` WHERE `player_name` ='".$_GET['message']."'"); /
if(mysql_num_rows($result) == 0) {
echo 'No user found!';
}
if(mysql_num_rows($result) > 0) {
echo 'Points added.';
$addxp = $row['race_xp'] = 75000; /
mysql_query("UPDATE `databasehere`.`wc3_player_race` SET `race_xp` = `race_xp` + ".$addxp." WHERE `wc3_player_race`.`player_id` ='".$result."' AND `wc3_player_race`.`race_id` =1");
}
?>
And error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Last edited by Linjon on Sat Feb 20, 2010 5:57 am, edited 1 time in total.
Post Reply