I have a table where I store information about users monthly activities. The table is as follows:
+-------------------------------------------+
|user_id | month | year | value1 | value2 |
+-------------------------------------------+
|1 | 10 | 15 | 45 | 78 |
+-------------------------------------------+
Let's say I have 3 users with ids 1, 2 and 3.
What I'd like to do is : when I submit the request to display records from the table for a given month and year, display the result above + create records with values set at "0" for that given month and year if user doesn't have record. It'd display this :
+-------------------------------------------+
|user_id | month | year | value1 | value2 |
+-------------------------------------------+
|1 | 10 | 15 | 45 | 78 |
|2 | 10 | 15 | 0 | 0 |
|3 | 10 | 15 | 0 | 0 |
+-------------------------------------------+
I managed to do it hardcoding it per user-id but I haven't been able to automate it to do: for each user that doesn't exist in ACTIVITIES where month=$month and year=$year insert (user_id,month,year,0,0).
This is what I got:
$result = mysqli_query($conn,"
SELECT
* FROM ACTIVITIES
WHERE
person_id = '2'
AND month = '$month'
AND year = '$year'
");
if(mysqli_affected_rows($conn) > 0)
{
}
else
{
mysqli_query($conn,"
INSERT INTO ACTIVITIES
(person_id, month, year, value1, value2)
VALUES
('2', $mois, $annee, '0', '0')");
}
Thx for the help. sb