MrVaux Posted August 26, 2010 Share Posted August 26, 2010 Dear PHP Guru I have been strugling with a piece of code I can´t get to work, so I hope someone can help me out! It is a simple league system, and what I wan´t is this: Two teams play each other 5 times, the one team who wins 3 or more gets 2 points. So my problem is this. When I add the score for the two teams say Team 1 wins 3 times and Team 2 wins 2 times, the db with this data updates fine. But I would also like a piece of code that that adds 2 points to the winning team. I have tried to do so below, but it doesn.t seems to work. I get no data in the tabel where the 2 points should go in to? What is wrong here? <? include "config.php"; $link = mysql_connect($dbhost, $dbuser, $dbpasswd) or die("Couldn't establish connection"); mysql_select_db($dbname); if (isset($id)) { $query = "UPDATE league_matchresults SET result_1 = '$result_1', result_2 = '$result_2' WHERE id = $id"; @$result = mysql_query($query, $link); echo mysql_error(); if($result) { if ($result_1 >= 3) { $query = "UPDATE league_teamdata SET points = points + 2 WHERE team_1 = $team_name"; echo $team_1; // This is only to make sure I have the correct data in $team_1 - Seems to work! } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/211766-can%C2%B4t-update-mysql-db-with-correct-data/ Share on other sites More sharing options...
MrVaux Posted August 26, 2010 Author Share Posted August 26, 2010 Really no one who knows what is wrong here Quote Link to comment https://forums.phpfreaks.com/topic/211766-can%C2%B4t-update-mysql-db-with-correct-data/#findComment-1103895 Share on other sites More sharing options...
Wolphie Posted August 26, 2010 Share Posted August 26, 2010 Try this: <?php include 'config.php'; if ($link = mysql_connect($dbhost, $dbuser, $dbpasswd)) { if (!mysql_select_db($dbname, $link)) { trigger_error('Could not select the database: ' . mysql_error(), E_USER_ERROR); } } else { trigger_error('Could not connect to the database: ' . mysql_error(), E_USER_ERROR); } if (isset($id)) { $query = "UPDATE league_matchresults SET result_1 = '%s', result_2 = '%s' WHERE id = %d"; $result = mysql_query(sprintf($query, mysql_real_escape_string($result_1), mysql_real_escape_string($result_2), mysql_real_escape_string($id) ), $link ); if ($result !== FALSE) { if ($result_1 >= 3) { $query = "UPDATE league_teamdata SET points = points + 2 WHERE team_1 = '%s'"; $result = mysql_query(sprintf($query, mysql_real_escape_string($team_name)), $link); if ($result !== FALSE) { echo $team_1; } else { trigger_error(mysql_error(), E_USER_ERROR); } } } else { trigger_error(mysql_error(), E_USER_ERROR); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/211766-can%C2%B4t-update-mysql-db-with-correct-data/#findComment-1103912 Share on other sites More sharing options...
MrVaux Posted August 26, 2010 Author Share Posted August 26, 2010 WOW That did the trick. Had the team_1 and team_name switched around, but thats not your bad. Figured that out so it works like a charm... But I must admit.... I have no idea what your code is about??? Do you know why it is not possible the way i coded it? Quote Link to comment https://forums.phpfreaks.com/topic/211766-can%C2%B4t-update-mysql-db-with-correct-data/#findComment-1103921 Share on other sites More sharing options...
Wolphie Posted August 26, 2010 Share Posted August 26, 2010 The reason why yours wouldn't work is because the update query you were using for the points addition, you didn't use mysql_query() to actually execute the query. Therefore the query wasn't being executed on the server and the database remained unchanged. Quote Link to comment https://forums.phpfreaks.com/topic/211766-can%C2%B4t-update-mysql-db-with-correct-data/#findComment-1103935 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.