mysql - PHP How to INSERT to Table Many Times in Once -


i have logical problem database, let me explain. there system:

  • i have users , admins.
  • admins can create project , if needed can add related users on project.
  • 1 user can work many projects , 1 project can have many users.(many many)

i build relational database , works fine. problem is, users working on project(s) ? don't know how insert them database. admins can choose users list , selected users should stored dependent project. couldn't managed insert database. here tables , codes.

--projects--      --users--        --projectusers-- userid            userid           userid   creationdate      registerdate     projectid projectid         email title             permission priority          name content           surname enddate           birthdate                   password  $sql="insert projects (         projectid,          userid,          creationdate,          title,         priority,         content,         enddate              )values(         '$projectid',          '$userid',          '$creationdate',          '$title',         '$priority',         '$content',         '$enddate'         )";          $result=mysql_query($sql) or die (mysql_error());           $sql2="insert projectusers (         projectid,          userid           )values(         '$projectid',          '$userid'         )";          $result2=mysql_query($sql2) or die (mysql_error()); 

the table "projectusers" stores 2 foreign keys. when admin creates project, userid , new projectid stores on "projectusers" , stores on "projects" table. admin choose users depended project, , can choose more 1 in once. how should insert dependent users on data base. , don't want store more 1 user in row. , should able answer queries:

  1. which users working on project(s) ?
  2. which users working on project ?
  3. this user working on which(s) project ?

example: admin creates new project , select 3 dependent users. want insert them table "projectusers".when admin creates project, want see "projectuser" table this:

userid    projectid 1          1 2          1 3          1 

p.s: "userid" on "projects" table shows me created project. owner.

remove userid projects table, you're storing information in projectusers association table anyway. every user working on project, have create separate row in projectusers table.

from there on, retrieval queries basic 3 table joins:

1. users working on project(s)?

select * projects p left join projectusers pu     on p.projectid = pu.projectid left join users u     on pu.userid = u.userid 

2. users working on project?

select * projects p left join projectusers pu     on p.projectid = 123 , p.projectid = pu.projectid left join users u     on pu.userid = u.userid 

3. user working on projects?

select * users u left join projectusers pu     on u.userid = 123 , u.userid = pu.userid left join projects p     on pu.projectid = p.projectid 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -