-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
?>
之前删除';'在线。 – Jens
谢谢。摆脱了那个错误。这是存储现有值的正确方法吗?因为如果看起来不起作用“SELECT SUM(蔬菜)AS totalvegetables,SUM(fullgrain)AS totalfullgrain,SUM(milk)AS totalmilk,SUM(water)AS totalwater FROM dietTbl ” – Christoffer
已在代码中看到太多错误在进行第一次修复之前 删除;从第24,45,71 – rocky