I am from Python-Django background now i am doing my project in PHP.
I have three tables in MySQL:
demo_user_selected_tags
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| tags_id | int(11) | NO | MUL | NULL | |
+---------+---------+------+-----+---------+----------------+
demo_user
+--------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_name | varchar(100) | NO | | NULL | |
| first_name | varchar(100) | NO | | NULL | |
| middle_name | varchar(100) | NO | | NULL | |
| last_name | varchar(100) | NO | | NULL | |
| image | varchar(5000) | YES | | NULL | |
| password | varchar(80) | NO | | NULL | |
| role | varchar(20) | NO | | NULL | |
| org_name_id | int(11) | NO | MUL | NULL | |
| timezone_id | int(11) | NO | MUL | NULL | |
| city | varchar(50) | YES | | NULL | |
| state | varchar(50) | YES | | NULL | |
| country | varchar(50) | YES | | NULL | |
| street | longtext | YES | | NULL | |
| pin | varchar(30) | YES | | NULL | |
| user_type | varchar(30) | NO | | NULL | |
| status | int(11) | YES | | NULL | |
| primary_mobile | varchar(100) | YES | | NULL | |
| secondary_mobile | varchar(100) | YES | | NULL | |
| primary_landline | varchar(100) | YES | | NULL | |
| secondary_landline | varchar(20) | YES | | NULL | |
| primary_email | varchar(100) | YES | | NULL | |
| secondary_email | varchar(100) | YES | | NULL | |
| notes | longtext | NO | | NULL | |
| date_created | datetime | YES | | NULL | |
| date_modified | datetime | YES | | NULL | |
+--------------------+---------------+------+-----+---------+----------------+
demo_tags
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| tags | varchar(150) | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+
At the time of user registration I am trying to store the tags for user. There can be more then one tags for a user.
So my question is how to write the SQL query so that my demo_user_selected_tags table also has a value?
You should use some ORM like Doctrine for example.
With Doctrine You define the table models and their relations with all the primary and foreign keys. After that it is enough to create an object of User, set its properties and tags and just save this User object – Doctrine will do the work for You and will fill the apropriate IDs (values) into all the related tables…
In other words, imagine we have a User object, that has many-to-many relation with Tags… We set some tags for this User and then save the User:
This is just an example on how this work could be done…