MySQL Query不会在PHP中更新数据库中的值

时间:2014-01-14 03:13:18

标签: php mysql sql

我在下面有一个if / else代码块,它应该调用具有特定参数的函数,具体取决于具体情况。该函数更新MySQL数据库中的特定值。但是,数据库值未更新。我做错了什么?

以下是我的代码:

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函数

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');
    }
}

这里有什么问题?查询错了吗?我在PHPMyAdmin中使用替换值运行查询并且它工作正常,因此它不能是查询。

1 个答案:

答案 0 :(得分:2)

WHERE Name = `:teamName`

如果这正是您脚本中的内容,那么您需要删除变量周围的反引号。

反引号用于引用字段名称。

some extend reading