0

I have an if/else block of code below that is supposed to call a function with specific parameters, depending on the situation. The function updates specific values in the MySQL database. However, the database values are not being updated. What am I doing wrong?

The following is my code:

process.php

$success = false;

$homeScore = $_POST['home'];
$awayScore = $_POST['away'];
$homeTeamName = $_POST['homeTeam'];
$awayTeamName = $_POST['awayTeam'];

try {
    $win = 0;
    $loss = 0;

    // HOME TEAM
    if ($homeScore > $awayScore)
    {
        $win = 1; $loss = 0;
        updateStandings($db7, $homeTeamName, $win, $loss);
        $win = 0; $loss = 1;
        updateStandings($db7, $awayTeamName, $win, $loss);
    }
    // AWAY TEAM
    elseif ($awayScore > $homeScore)
    {
        $win = 1; $loss = 0;
        updateStandings($db7, $awayTeamName, $win, $loss);
        $win = 0; $loss = 1;
        updateStandings($db7, $homeTeamName, $win, $loss);
    }

    $success = $_SERVER['HTTP_REFERER'];
}
catch (Exception $e)
{
    $success="/error";
}
header("Location: " . $success);

function updateScore($db, $gameID, $home, $away)
{
    $db -> updateScoreForGame($gameID, $home, $away);
}
function updateStandings($db, $teamName, $win, $loss)
{
    $db -> updateLeagueStandings($teamName, $win, $loss);
}

updateLeagueStandings function

public function updateLeagueStandings($teamName, $win, $loss) {
    try {
        $sth = $this -> db -> prepare("UPDATE teams SET wins = wins + (:winsNum), losses = losses + (:lossesNum) WHERE Name = `:teamName`");
        $sth->bindParam(':winsNum', $win, PDO::PARAM_INT);
        $sth->bindParam(':lossesNum', $loss, PDO::PARAM_INT);
        $sth->bindParam(':teamName', $teamName, PDO::PARAM_STR);
        $sth -> execute();
    } catch (Exception $e) {
        header('Location: /error');
    }
}

What's wrong here? Is the query wrong? I ran the query with substituted values in PHPMyAdmin and it worked fine, so it can't be the query.

2
  • $homeTeamName > $awayTeamName --- isn't this their name. Shouldn't you be comparing the scores? $homeScore = $_POST['home']; $awayScore = $_POST['away']; Commented Jan 14, 2014 at 3:15
  • 1
    I apologize, I mis-copied that part of the code here. The code I have already says: if ($homeScore > $awayScore)...elseif ($awayScore > $homeScore) Commented Jan 14, 2014 at 3:19

1 Answer 1

2
WHERE Name = `:teamName`

If this is what exactly in your script, then you need to remove the backtick quote around the variable.

The backticks are used to quote field names.

some extend reading

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.