Page 1 of 2
Whats wrong?
Posted: Fri Feb 12, 2010 11:21 am
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);
}
?>
Re: Whats wrong?
Posted: Fri Feb 12, 2010 11:28 am
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).
Re: Whats wrong?
Posted: Fri Feb 12, 2010 12:18 pm
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.
Re: Whats wrong?
Posted: Fri Feb 12, 2010 2:47 pm
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.
Re: Whats wrong?
Posted: Fri Feb 12, 2010 3:43 pm
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.';
}
Re: Whats wrong?
Posted: Sat Feb 13, 2010 1:35 am
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.
Re: Whats wrong?
Posted: Sat Feb 13, 2010 2:27 am
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');
Re: Whats wrong?
Posted: Sat Feb 13, 2010 5:15 am
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?
Re: Whats wrong?
Posted: Sat Feb 13, 2010 10:28 am
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."

Him posting his schema confirms that is his problem.
Re: Whats wrong?
Posted: Sat Feb 13, 2010 12:34 pm
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.
Re: Whats wrong?
Posted: Sun Feb 14, 2010 3:45 pm
by josh
Was rolling my eyes at him not you
Re: Whats wrong?
Posted: Sun Feb 14, 2010 3:50 pm
by social_experiment
Ah. Apologies for my misdirected animosity.
Re: Whats wrong?
Posted: Mon Feb 15, 2010 7:02 am
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?
Re: Whats wrong?
Posted: Mon Feb 15, 2010 10:00 am
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

Re: Whats wrong?
Posted: Fri Feb 19, 2010 2:55 pm
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