Php if/else语句中出错
我需要一些帮助使if/else语句在mysql中工作 if语句工作正常,else语句出错。我是布劳尔斯告诉我的 “解析错误:语法错误,在/var/www/domane/public\u html/app/save.php的第48行出现意外的'else'(T_else)”-哪一行是else 它应该获取行的当前值,然后将其添加到新值并更新它Php if/else语句中出错,php,mysql,Php,Mysql,我需要一些帮助使if/else语句在mysql中工作 if语句工作正常,else语句出错。我是布劳尔斯告诉我的 “解析错误:语法错误,在/var/www/domane/public\u html/app/save.php的第48行出现意外的'else'(T_else)”-哪一行是else 它应该获取行的当前值,然后将其添加到新值并更新它 <?php $dsn = "databasename"; $username="username"; $password="password"; t
<?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
?>
这部分代码:
};
//------------------------ If it already exists, update dietTbl ----------
else {
删除“;”删除代码>之前在线。谢谢。摆脱了那个错误。这是存储现有值的正确方法吗?因为如果似乎不起作用,“选择SUM(蔬菜)作为TotalGreens,SUM(fullgrain)作为totalfullgrain,SUM(milk)作为totalmilk,SUM(water)作为DietBl中的totalwater”在进行第一次修复之前已经在代码中看到太多错误;从第24行,第45行,71@Christoffer如果使用sum,则必须使用group by。使其有效。感谢评论中已经提到的一点