Jump to content

Query two tables and Update field


ptmuldoon

Recommended Posts

Hi.

First post here, and I'm pretty new to php as well, and am hoping someone can possible help with my current issue.

I'm trying to add to a users table field, the number of players the user has faced in a game.  So I'm trying to count the number of players in a game_xx table, and then add that number to the users table.  The game table users 'player' name, and the users table users a 'login' name.  Thus, I'm pretty sure I need to match the player name with the login name as well.

I think I'm close but the below code is not adding it. 

[code=php:0]// Add total players in game to user table
$sql = 'SELECT player FROM game_'.$_SESSION['game_id'];
$ply_name = get_one($sql);

$sql = 'SELECT login FROM users';
$login = get_one($sql);

$sql = 'SELECT COUNT(player) FROM game_'.$_SESSION['game_id'];
$players = get_one($sql);

$sql = 'SELECT total_players FROM users WHERE \''.$login.'\' = \''.$ply_name.'\' ';
$total_plyr = get_one($sql);
$total_player = "$total_plyr + $players";

$sql = 'UPDATE users SET total_players = '.$total_player.' WHERE \''.$login.'\' = \''.$ply_name.'\' ';
$result = single_qry($sql);[/code]
Link to comment
https://forums.phpfreaks.com/topic/34797-query-two-tables-and-update-field/
Share on other sites

[quote author=Daniel0 link=topic=123050.msg509128#msg509128 date=1169297255]
I don't get your database layout and what does get_one() do?
[/quote]

Sorry about that.  get_one is just a custom function of:[code=php:0]//retrieves 1 piece of data
function get_one($string) {
$q = mysql_query($string) or die('Failed to get_one: '.mysql_error());
while ($row = mysql_fetch_array($q)) { $result = $row[0]; }
return $result;
}[/code]

and single_query is:[code=php:0]function single_qry($string) {
$result = mysql_query($string) or die('Failed to query: '.mysql_error());
return $result;
}[/code]

I'm still learning php, so I'm not sure I need to use type of query to get the data or not.  I can abandon those custom functions too if necessary.

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.