-2

I need some help making an if/else statement work in mysql The if statement works proper, but the else statement buggs. I browers tells me

"Parse error: syntax error, unexpected 'else' (T_ELSE) in /var/www/domane/public_html/app/save.php on line 48" - which is the else line

It's supposed to get the current value of the row, and then add it to the new value and update it

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

?>
frisk0k
  • 157
  • 1
  • 1
  • 11
  • 2
    Remove the `;` on line before. – Jens Oct 05 '15 at 11:09
  • Thanks. Got rid of that error. Is it the proper way to store the existing values? Because if doesn't seem to work "SELECT SUM(vegetables) AS totalvegetables, SUM(fullgrain) AS totalfullgrain, SUM(milk) AS totalmilk, SUM(water) AS totalwater FROM dietTbl " – frisk0k Oct 05 '15 at 11:14
  • Already seen too many error in your code before going for that first fix Remove ; from line 24, 45, 71 – rocky Oct 05 '15 at 11:15
  • @Christoffer you have to use group by if you use sum. – Jens Oct 05 '15 at 11:17
  • Made it work. Thanks a lot – frisk0k Oct 05 '15 at 12:44

1 Answers1

0

From this part of code:

};

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

else {

remove ";"