farahZ Posted May 15, 2013 Share Posted May 15, 2013 i have 2 tables: fooddiary and food food: FID, Food, Size, Calories fooddiary: ID, Date, DayTime, FoodName, Calories user is selecting names of food , they are saved in $_SESSION['foodTypes'] i want to get the calories of each food he chose, add them to a total, and then save them as a row in the fooddiary table the problem is with the first query, its not getting me the calories i'm having this: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean foreach ($_SESSION['foodTypes'] as $food) { $cal=0; $query = "SELECT Calories FROM food where Food=$food"; $result1 = mysqli_query($con, $query); $row = mysqli_fetch_array($result1); $cal=(int)$row['Calories']; $total= $total + (int) $row['Calories']; $sql=""; $sql = "INSERT INTO fooddiary (ID, Date, DayTime, FoodName, Calories) VALUES ($clid, '$date', 'Breakfast', '$food', $cal) ON DUPLICATE KEY UPDATE FoodName = '$food', Calories=$cal"; var_dump($sql); } Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 15, 2013 Share Posted May 15, 2013 $sql = "INSERT INTO fooddiary (ID, Date, DayTime, FoodName, Calories)VALUES ('$clid', '$date', 'Breakfast', '$food', '$cal')ON DUPLICATE KEY UPDATE FoodName = '$food', Calories=$cal"; try the chance, then echo $sql Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 15, 2013 Author Share Posted May 15, 2013 i ddnt understand you !you ddnt change the code? Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted May 15, 2013 Solution Share Posted May 15, 2013 your select query is failing due to an sql (mysql) syntax error. you need to echo mysqli_error($con); to find out why. if i'm not mistaken, your food column contains strings and the $food variable/value needs to be enclosed in single quotes so that it is treated as a string - '$food' Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 15, 2013 Share Posted May 15, 2013 the reply by Q695 has nothing to do with your problem. it's not even where the error is occurring at. Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 15, 2013 Author Share Posted May 15, 2013 i echoed itWarning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean at this line: $row = mysqli_fetch_array($result1); Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 15, 2013 Author Share Posted May 15, 2013 problem solved concerning this error but data isn't inserted !! (i enclosed $food in single quotes) Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 15, 2013 Share Posted May 15, 2013 the code you posted isn't running the insert query, unless you didn't post all the relevant code. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.