Jump to content

retrieve data from database.. variable in the query


farahZ
Go to solution Solved by DarkKnight2011,

Recommended Posts

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"

Edited by farahZ
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

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.