0

I have 2 tables in my database: companies and employees. Each company can have multiple employees and each employee can work at multiple companies. Therefore I need a many-to-many relational table called companies_employees.

I think I understand the 'retrieve' and 'delete' parts of CRUD but how do you 'create' and 'update'? Let's say the user adds an employee to a company. What would the code be?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alex Jolley
  • 11
  • 1
  • 2
  • Can you please explain what part of the code you need help with? A proper complete CRUD application is not something that can be described in an answer. – Álvaro González Aug 07 '18 at 18:52
  • Yes, I'm not asking for a complete CRUD application. Like I said, I just want to figure out how to insert into the database using a junction table. – Alex Jolley Aug 07 '18 at 19:01
  • 1
    You will need to insert into the `User` table to add the new user, retrieving back the ID (assuming an autoincrement is in place) then you will insert a new record into your junction table for that company and your new user id. Now just write the code to do that. [This may be of some help](http://php.net/manual/en/mysqli.insert-id.php) – JNevill Aug 07 '18 at 20:30
  • I'm certainly not an expert, but I'm fairly certain if I want to create a new record in companies or employees tables I don't INSERT INTO the users table. I don't want to add a new user. That's not even what my question is about at all. – Alex Jolley Aug 09 '18 at 13:46

0 Answers0