Jump to content

query connection problem


farahZ
Go to solution Solved by mac_gyver,

Recommended Posts

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);
}
Link to comment
Share on other sites

$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

Link to comment
Share on other sites

  • Solution

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'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.