2014-01-14 81 views
0

我有一個if/else以下的代碼塊,根據具體情況應該調用具有特定參數的函數。該函數更新MySQL數據庫中的特定值。但是,數據庫值沒有被更新。我究竟做錯了什麼?MySQL查詢不更新數據庫中的值在PHP中

以下是我的代碼:

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

$ homeTeamName> $ awayTeamName ---不是他們的名字。你不應該比較分數嗎? $ homeScore = $ _POST ['home']; $ awayScore = $ _POST ['away']; – pathfinder

+1

我很抱歉,我在這裏錯誤地複製了那部分代碼。我已經說過的代碼:'如果($ homeScore> $ awayScore)... elseif($ awayScore> $ homeScore)' – DemCodeLines

回答

2
WHERE Name = `:teamName` 

如果這正是您的腳本中的內容,那麼您需要刪除圍繞該變量的反引號引號。

反引號用於引用字段名稱。

some extend reading

+0

這樣做的竅門!謝謝!我會確保我讀到了。 – DemCodeLines