Click here to Skip to main content
15,303,572 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I am building an attendant system, and I have two tables (students- attend)
So, all the students will be registered at the student table let’s say we only have five students as shown in the photo.
When the student signed in, he/she will be inserted to attend table as shown in the photo and the word present will be written under attend table head.
But I want when I click a button after the class finish to insert the rest of the student that didn’t sign in in this case (Sara and Zain) to attend table with the word absent. How I can do that.


Here is the photo
Thank you

What I have tried:

I succeed when inserting the present student. I tried to do a join between tables but can't do it correctly + I don't know if joining between tables is the perfect solution and if yes I don't know how to ably it correctly.
Posted
Updated 17-Jan-22 10:28am

Try the following query to insert absent students into attend table on a button click:
'Day_date: 2022-01-17 is the control date on which you are performing action. You can pass the parameter to run this query'

SQL Query:
INSERT INTO attend
SELECT Id, Number, fullName, 'absent', '2022-01-17'
FROM student 
WHERE Id NOT IN (SELECT Stu_id FROM attend WHERE Day_date = '2022-01-17')


MySQL Query:

INSERT INTO attend
SELECT Id, Number, fullName, 'absent', '2022-01-17'
FROM student 
WHERE Id NOT IN (SELECT Stu_id FROM attend WHERE Day_date = '2022-01-17')
   
Comments
Maciej Los 17-Jan-22 16:22pm
   
As far as i understand, you OP is able to insert data into another table...
M Imran Ansari 17-Jan-22 16:29pm
   
Yes. As per problem statement "insert the rest of the student that didn’t sign in into attend table"
Hisham Alosaimi 17-Jan-22 17:32pm
   
yes I want the rest of the students that are not at attend table, I will try the query and see its result. Thank you.
Hisham Alosaimi 17-Jan-22 17:53pm
   
I write this query and when I execute it, doesn't insert anything to the attend table:
$q = INSERT INTO attend SELECT id, fullName, number, 'absent', 'curdate()'
FROM student
WHERE id NOT IN (SELECT stu_id FROM attend WHERE day_date = 'curdate()')";

curdate = the current date because I inserted date with current date and system will take the attendant once every day.
M Imran Ansari 18-Jan-22 5:59am
   
What is value of curdate()? and check the format of date which already exists inthe attend table.
Hisham Alosaimi 18-Jan-22 6:54am
   
It is the current date of the day.
I already use curdate() when I insert present and it work just fine
M Imran Ansari 18-Jan-22 6:55am
   
Have you checked the date format of inserted records of attend table?
Hisham Alosaimi 18-Jan-22 6:57am
   
Yeah, it is a date format, I uploaded in my original question an image that contains the table structure that I took a screenshot of my tables in PHPMyAdmin designer.
M Imran Ansari 18-Jan-22 8:10am
   
In your attached image date format of Day_date = '2022-01/17' but when we call CURDATE() it returns '2022-01-17'. Please check the format of inserted data.
CHill60 18-Jan-22 8:10am
   
Is it a "Date" or a "Datetime" - if the latter then you can't use "equals"
Hisham Alosaimi 18-Jan-22 8:19am
   
I use this query when the student signs in to attend and it works just fine using curdate in it.

INSERT INTO attend (std_id,number, stuName, attend, day_date) VALUES ('$stID','$stuID', '$fullname', 'present', curdate())
M Imran Ansari 18-Jan-22 8:29am
   
After this query Day_date should be like '2022-01-18' and see your attend table (as per attached image) date is saving like '2022-01/17'. Why so?
Hisham Alosaimi 18-Jan-22 8:46am
   
It is a typo I wrote these two tables in MS Word for for the explaining purpose. But the two tables with green line I took screen shot from my database on phpmyadmin
If i understand you correctly... you want to display all 5 records from attend table and corresponding data from student table.

So, use LEFT JOIN:
SQL
SELECT A.*, B.*
FROM attend AS A
LEFT JOIN student AS B ON A.number = B.number


For further details, please see:
Visual Representation of SQL Joins[^]
SQL Joins[^]
   
You do NOT copy names from one table to another. That's duplicating data and it's wasteful.
If you need to change the state of a student in the "attend" table, you put the ID of the student in that table, not the student's name.
   
Comments
Hisham Alosaimi 17-Jan-22 17:31pm
   
but I am using a PHP spreadsheet to print out the content of the attend table to xlxs file and I achieve that + I am not very good with MySQL and I am still trying to understand and ably the concept of joining tables together.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900