我在下面有一个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中使用替换值运行查询并且它工作正常,因此它不能是查询。
答案 0 :(得分:2)