2015-10-05 91 views
-2

我需要一些幫助,使if/else語句在mysql中工作 if語句正常工作,但else語句錯誤。我的browers告訴我if/else語句中的錯誤MySQL

「解析錯誤:在/var/www/domane/public_html/app/save.php線48上的語法錯誤,意想不到的‘其他’(T_ELSE)」 - 這是其他行

它應該得到該行的當前值,然後將其添加到新的值,並更新

<?php 

$dsn = "databasename"; 
$username="username"; 
$password="password"; 

try { 
    $conn = new PDO($dsn, $username, $password); 
    $conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} catch(PDOException $e) { 
    echo "Connection failed: ".$e->getMessage(); 
} 

//------------------------ Does the category already exist in dietTbl? ------------------------- 
$sql="SELECT COUNT(*) AS subjectcount FROM dietTbl WHERE day=CURDATE()"; 

try { 
          $st = $conn->prepare($sql); 
          $st->bindValue(":mainsubject",$mainSubject, PDO::PARAM_STR); 
          $st->execute(); 
          $row=$st->fetch(); 
          $subjectcount=$row["subjectcount"]; // if >0 the yes, the category already exists 
} catch (PDOException $e) { 
          echo "Server Error - try again!".$e->getMessage(); 
}; 

//------------------------ If it dosn't, insert it into dietTbl ------------------------- 
if ($subjectcount==0) { 

$sql="INSERT INTO dietTbl (day, vegetables, fullgrain, milk, water) values (:day, :vegetables, :fullgrain, :milk, :water)"; 

try { 

    $st = $conn->prepare($sql); 
    $st->bindValue(":day",$_POST["day"], PDO::PARAM_STR); 
    $st->bindValue(":vegetables",$_POST["vegetables"], PDO::PARAM_STR); 
    $st->bindValue(":fullgrain",$_POST["fullgrain"], PDO::PARAM_STR); 
    $st->bindValue(":milk",$_POST["milk"], PDO::PARAM_STR); 
    $st->bindValue(":water",$_POST["water"], PDO::PARAM_STR); 

    $st->execute(); 
          } catch (PDOException $e) { 
                  echo "Server Error - try again!".$e->getMessage(); 
          } 
}; 

//------------------------ If it already exists, update dietTbl ------------------------- 
else { 

SELECT SUM(vegetables) AS totalvegetables, SUM(fullgrain) AS totalfullgrain, SUM(milk) AS totalmilk, SUM(water) AS totalwater FROM dietTbl 



    $sql="UPDATE INTO dietTbl (vegetables, fullgrain, milk, water) values (:vegetables+totalvegetables, :fullgrain+totalfullgrain, :milk+totalmilk, :water+totalwater)"; 

try { 

    $st = $conn->prepare($sql); 
    $st->bindValue(":vegetables",$_POST["vegetables"], PDO::PARAM_STR); 
    $st->bindValue(":fullgrain",$_POST["fullgrain"], PDO::PARAM_STR); 
    $st->bindValue(":milk",$_POST["milk"], PDO::PARAM_STR); 
    $st->bindValue(":water",$_POST["water"], PDO::PARAM_STR); 
    $st->execute(); 
          } catch (PDOException $e) { 
                  echo "Server Error - try again!".$e->getMessage(); 
          } 




}; 

echo "Information saved"; 
$conn=null; //Close database connection 

?> 
+2

之前刪除';'在線。 – Jens

+0

謝謝。擺脫了那個錯誤。這是存儲現有值的正確方法嗎?因爲如果看起來不起作用「SELECT SUM(蔬菜)AS totalvegetables,SUM(fullgrain)AS totalfullgrain,SUM(milk)AS totalmilk,SUM(water)AS totalwater FROM dietTbl 」 – Christoffer

+0

已在代碼中看到太多錯誤在進行第一次修復之前 刪除;從第24,45,71 – rocky

回答

0

從這部分代碼:

}; 

//------------------------ If it already exists, update dietTbl ---------- 

else { 

刪除「;」

+0

已經在評論中提到 – rocky