Forum Moderators: coopster

Message Too Old, No Replies

Adding to an existing sql value.

         

jamesjohnson88

6:20 pm on Jul 12, 2009 (gmt 0)

10+ Year Member



I'm currently trying to write some software to allow the creation of leagues, to circumvent all the mysql creation so that any idiot can set up a league and add scores, etc.

I have the table creation working, players can be added/removed, thats all fine. The part which i'm stuck on and can't seem to find an useful information about is adding to an existing value in a sql table.

Here is the current code for addscore.php -

<?php
//SCORE ADDITION CODE.

//Connect & Select.
$con = mysql_connect("localhost","u08105199","edited");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("u08105199", $con);

//Declare variables.
$leagueName = mysql_real_escape_string($_POST['leagueName'])
$player1 = mysql_real_escape_string($_POST['player1'])
$player2 = mysql_real_escape_string($_POST['player2'])
$result = mysql_real_escape_string($_POST['result'])
//Get timestamp variable.
$timeStamp=date("d/m/Y")
//Find winner.
switch ($result)
{
case "defeated":
$winner = $player1;
$loser = $player2;
break;
case "lost to":
$winner = $player2;
$loser = $player1;
break;
case "drew with":
$draw1 = $player1;
$draw2 = $player2;
break;
default:
echo "No result selected!";
}

if ($winner = $player1)
{
//Code to be executed if condition is true;
mysql_query("UPDATE $leagueName SET GamesPlayed=GamesPlayed+1, Wins=Wins+1, Points=Points+3 WHERE PlayerName = {$player1}");
mysql_query("UPDATE $leagueName SET GamesPlayed=GamesPlayed+1, Losses=Losses+1 WHERE PlayerName = {$player2}");

//End of snippet.
}
elseif ($winner = $player2)
{
//Code to be executed if condition is false;
}
else
{
//Default execution.
}

//RESULT LOG CODE.
//Convert score into a string.
$resultString=('$player1' + '$result' + '$player2');

//Open resultlog.
$file=fopen("resultlog.php","r+") or exit("Unable to open file!");

//Update resultlog with new result.
// TO DO!

//Close resultlog.
fclose($file);

//Close connection.
mysql_close($con);
?>

Basically, I want the code to determine the winner from the switch statement, then use the if statement to update the table with the new stats.

I.E - The winner gets +1 games played, +1 win & +3 point.
And the loser, well you get the idea.

Any idea's on where i'm going wrong.

jamesjohnson88

8:21 pm on Jul 12, 2009 (gmt 0)

10+ Year Member



Latest version of code, same problem but closer to working...I think,

<?php
//SCORE ADDITION CODE.

//Connect & Select.
$con = mysql_connect("localhost","u08105199","edited");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("u08105199", $con);

//Declare variables.
$leagueName = mysql_real_escape_string($_POST['leagueName'])
$player1 = mysql_real_escape_string($_POST['player1'])
$player2 = mysql_real_escape_string($_POST['player2'])
$result = mysql_real_escape_string($_POST['result'])
//Get timestamp variable.
$timeStamp=date("d/m/Y")
//Find winner.
switch ($result)
{
case "defeated":
$winner = "player1";
$loser = "player2";
break;
case "lost to":
$winner = "player2";
$loser = "player1";
break;
case "drew with":
$draw1 = "player1";
$draw2 = "player2";
break;
default:
echo "No result selected!";
}

if ($winner = "player1")
{
mysql_query("UPDATE $leagueName SET GamesPlayed=GamesPlayed+1, Wins=Wins+1, Points=Points+3 WHERE PlayerName = {$player1}");
mysql_query("UPDATE $leagueName SET GamesPlayed=GamesPlayed+1, Losses=Losses+1 WHERE PlayerName = {$player2}");
echo "Score has successfully been added to the database.";
}
elseif ($winner = "player2")
{
mysql_query("UPDATE $leagueName SET GamesPlayed=GamesPlayed+1, Wins=Wins+1, Points=Points+3 WHERE PlayerName = {$player2}");
mysql_query("UPDATE $leagueName SET GamesPlayed=GamesPlayed+1, Losses=Losses+1 WHERE PlayerName = {$player1}");
echo "Score has successfully been added to the database.";
}
else
{
mysql_query("UPDATE $leagueName SET GamesPlayed=GamesPlayed+1, Draws=Draws+1, Points=Points+1 WHERE PlayerName = {$player1}");
mysql_query("UPDATE $leagueName SET GamesPlayed=GamesPlayed+1, Draws=Draws+1, Points=Points+1 WHERE PlayerName = {$player1}");
echo "Score has successfully been added to the database.";
}

/*
//RESULT LOG CODE.
//Convert score into a string.
$resultString=('$player1' + '$result' + '$player2');

//Open resultlog.
$file=fopen("resultlog.php","r+") or exit("Unable to open file!");

//Update resultlog with new result.
// TO DO!

//Close resultlog.
fclose($file);
*/

//Close connection.
mysql_close($con);
?>