Jump to content

retrieve data from database.. variable in the query


farahZ

Recommended Posts

in this case your using a variable that hasn't been defined before, so either your calculate function isn't working or you need to initialise it first, ie. add $calories = 0; near the top of your code before you use it in a calculation

Link to comment
Share on other sites

ok, i initialized everything but how to initialize a query?? empty string ddnt work out!

 

string(181) "INSERT INTO caloriescounter (ID, Date, Lunch, Calories) VALUES (111, '05.07.13',',Cucumber without peal', 65) ON DUPLICATE KEY UPDATE Lunch = ',Cucumber without peal', Calories=65"

Link to comment
Share on other sites

i told you before that after every action i get a notice .. so i intialized every variable

but i dont know how to initizlize this ... 

 

 

$sql = "INSERT INTO caloriescounter (ID, Date, Lunch, Calories)
VALUES ($clid, '$date','$foodS', $total)
ON DUPLICATE KEY UPDATE Lunch = '$foodS', Calories=$total";

its working fine, its just i'm getting this after its been executed
 

 

string(181) "INSERT INTO caloriescounter (ID, Date, Lunch, Calories) VALUES (111, '05.07.13',',Cucumber without peal', 65) ON DUPLICATE KEY UPDATE Lunch = ',Cucumber without peal', Calories=65"

 

Link to comment
Share on other sites

@farahZ

 

As I said previously, you should normalize your data tables. Also you shouldn't store totals in your tables.

You have your food table containing calories
 

+--------+-----------------+----------+
| idfood | food_name       | calories |
+--------+-----------------+----------+
|      1 | apple           |       20 |
|      2 | avocado         |      320 |
|      3 | cheese, cheddar |      240 |
|      4 | cucumber        |       12 |
|      5 | egg             |       50 |
|      6 | salmon          |      120 |
|      7 | steak 8oz       |      185 |
+--------+-----------------+----------+


what you should store for the meals is
 

+-------+--------+------------+-----------+--------+
| idlog | iduser | date_eaten | meal      | idfood |
+-------+--------+------------+-----------+--------+
|     1 |    111 | 2013-05-07 | breakfast |      1 |
|     2 |    111 | 2013-05-07 | breakfast |      3 |
|     3 |    111 | 2013-05-07 | lunch     |      4 |
|     4 |    111 | 2013-05-07 | lunch     |      5 |
|     5 |    111 | 2013-05-07 | lunch     |      6 |
+-------+--------+------------+-----------+--------+


When you want the totals, query the tables, EG
 

SELECT fe.meal, SUM(f.calories) as calories 
FROM food_log fe
    INNER JOIN food f USING (idfood)
WHERE fe.iduser = 111 AND fe.date_eaten = '2013-05-07'
GROUP BY meal;


+-----------+----------+
| meal      | calories |
+-----------+----------+
| breakfast |      260 |
| lunch     |      182 |
+-----------+----------+

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.