简体   繁体   English

HTML复选框,用于在多对多查找表上添加/删除条目(PHP / MySQL)

[英]HTML check boxes to add/remove entries on a many-to-many look up table (PHP/MySQL)

I have the following entities: 我有以下实体:

EMPLOYEE (id, fname, lname, ...) 雇员(id,fname,lname,...)

PROJECT (id, ...) 项目(id,...)

EMPLOYEE_PROJECT (employee_id, project_id, ...) EMPLOYEE_PROJECT(employee_id,project_id等)

An employee can work on more than one project, and a project can be worked on by more than one employee, so the EMPLOYEE_PROJECT table is used to relate them. 一名员工可以从事多个项目,而一个项目可以由多个员工从事,因此使用EMPLOYEE_PROJECT表将它们关联起来。

I would like to generate in my application (PHP/MySQL) a form for a given PROJECT ID with a checkbox next to each employee name, that will display as checked if an entity for that employee id exists for that project id in the EMPLOYEE_PROJECT table, and that will allow the user to check or uncheck any employee and then click an "update" button to add/remove the selected/unselected employees from the project by adding/removing the entries on the EMPLOYEE_PROJECT table. 我想在我的应用程序(PHP / MySQL)中生成给定PROJECT ID的表单,每个雇员姓名旁边都有一个复选框,如果在EMPLOYEE_PROJECT表中该项目ID是否存在该雇员ID的实体,该表单将显示为选中状态,这将允许用户选中或取消选中任何员工,然后单击“更新”按钮,以通过添加/删除EMPLOYEE_PROJECT表中的条目来从项目中添加/删除所选/未选中的员工。

I am able to use an IF statement to display "checked" on any entity that exists in the EMPLOYEE_PROJECT table. 我能够使用IF语句在EMPLOYEE_PROJECT表中存在的任何实体上显示“已选中”。 My problems are: 我的问题是:

  1. Properly forming the query to get the list. 正确形成查询以获取列表。 If I get the list of employees by a JOIN of EMPLOYEE ON EMPLOYEE_PROJECT, it only shows employees who have been involved in a project, not all employees. 如果我通过EMPLOYEE_PROJECT上的EMPLOYEE的JOIN获得员工列表,则该列表仅显示参与项目的员工,而不是所有员工。 I can do a LEFT JOIN to show all, but that would repeat names of employees who have multiple projects, and I only want to show each name once. 我可以做一个LEFT JOIN来显示全部,但是那会重复有多个项目的员工的名字,我只想显示每个名字一次。 If I add a GROUP BY employee.id, I can get a proper list of only one of each employee, however the results don't necessarily match the correct project_id. 如果添加GROUP BY employee.id,则只能获得每个员工中只有一个的正确列表,但是结果不一定与正确的project_id相匹配。 If I add a HAVING with the project_id, I lose employees who are not on the project (keep in mind I want to list ALL employees, but only show a check next to the ones that are currently on the project). 如果我添加带有project_id的HAVING,则会丢失不在项目中的员工(请记住,我想列出所有员工,但仅在项目中当前员工旁边显示一个支票)。

  2. Assuming I figure out #1 and can list all employees and display a check next to the ones which exist as an EMPLOYEE_PROJECT entity for the specified project, how can I allow the check boxes to be modified by the user, and then submitted and have the one submit action add/remove the relevant entities from the EMPLOYEE_PROJECT table? 假设我找出了#1,并且可以列出所有雇员,并在作为指定项目的EMPLOYEE_PROJECT实体存在的雇员旁边显示支票,那么我如何允许用户修改复选框,然后提交并让其一个提交动作从EMPLOYEE_PROJECT表中添加/删除相关实体?

Thanks so much!! 非常感谢!!

@zzarbi is correct for #2. @zzarbi对于#2是正确的。 For #1 you need a sub-query: 对于#1,您需要一个子查询:

SELECT employee.*, (SELECT count(id) FROM employee_project 
   WHERE project_id=<project_id> AND employee_id=employee.id) 
   AS in_project 
   FROM employee

For #1 "list ALL employees, but only show a check next to the ones that are currently on the project". 对于#1,“列出所有雇员,但仅在项目中当前雇员旁边显示一个检查”。 (That work only if you show only one project at the time) (仅当您一次仅显示一个项目时,该功能才有效)

SELECT * from employee as e
LEFT JOIN EMPLOYEE_PROJECT as ep on ep.employee_id = e.id
WHERE ep.project_id = <project id>

in php : 在php中:

if("employee_id" IS NOT NULL/empty){
    echo "check";
}

For #2, usually my middle table project_employee has nothing else than two id (employeeId, projectId). 对于#2,通常我的中间表project_employee除了两个id(employeeId,projectId)外没有其他东西。 So i generally just delete every employee for this projectId and re-add them. 因此,我通常只删除此projectId的每个员工,然后重新添加它们。 If you have other data in the same table, this might not work. 如果同一表中还有其他数据,则此方法可能无效。 And you will have to compare old data store to new data to be stored. 而且,您必须将旧数据存储与要存储的新数据进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM