Skip to content
Advertisement

On signup form INSERT -> update corresponding values in another table

From usersTable there are userRoles. — Teacher, Mentor, Student.

When a user submits signup form, they are either one of the three.

I also have three more tables, — teacherTable, mentorTable, studentTable.

These three tables have FK’s for “userID” which is the PK in userRoles.

Is there a way to auto INSERT name, lastname, email etc. into the teacher, mentor or student tables (depending on the userRole) so that the name, lastname, email etc. match with the “userID” from the main userTable?

I want some way to efficiently do this, rather than having to use INSERT multiple times and making it very long. Can anyone show me how to do this simply as I am very new to this.

Advertisement

Answer

Use a stored procedure. But as @Barmar pointed out, you shouldn’t duplicate the data. The only columns that belong in the teacherTable, mentorTable and studentTable are for information that is unique to those roles. So, for example a Teacher might have an employee number and department number, and status level (“full professor, etc), while a student might have a Student ID that might even overlap the employee ID numbers.

But back to using stored procedures… not only can you insert into multiple tables, you avoid issues of SQL injection. Note that after you insert into your userTable, you will want the stored procedure to save the last id entered using SELECT LAST_INSERT_ID() in order to use that to create the record in the second table entry.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement