if / else语句MySQL出错

时间:2015-10-05 11:07:11

标签: php mysql

我需要一些帮助,使得if / else语句在mysql中运行 if语句正常工作,但else语句错误。我的浏览器告诉我

“解析错误:语法错误,第48行/var/www/domane/public_html/app/save.php中的意外'其他'(T_ELSE)” - 这是其他行< / p>

它应该获取行的当前值,然后将其添加到新值并更新它

<?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

?>

1 个答案:

答案 0 :(得分:0)

从这部分代码:

};

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

else {

删除“;”