2015-09-05 19:59
浏览 925


I am working on an existing HTML form used to collect data about a project and then inserts that project record into a MySQL database using PHP.

Inside the form, there is an input field named "staff[]". This field is a multi select element, that allows users to select more than one team member to handle the project.

<form action="" method="post">
    <select multiple name="staff[]">
        <option value="1">Mary</option>
        <option value="2">Tyrone</option>
        <option value="3">Rod</option>
        <option value="4">Marcus</option>
        <option value="5">David</option>

For example purposes, the user selects Tyrone, Rod and David for this particular project. If we insert the record at this point, the database only stores the first record value, which would be Tyrone's ID of 2. General practice is to store each instance in a separate table, however this is not our system and due to a restriction of 4 members for each project, management would prefer we insert a comma delimited array into each project's staff column for convenience.

In order to handle this issue, we've created a foreach loop that loops through the selected values from the dropdown menu, while ensuring a trailing comma doesn't exist:

// Add array into one variable
$staff_count = count($_POST['project_staff']);
$i = 0;
foreach($_POST['project_staff'] as $staff) {
    if (++$i === $staff_count) {
        $member_variable .= $staff;
    } else {
        $member_variable .= $staff . ", ";

After pressing the submit button, the above script is ran (which produces an array value of (2, 3, 5)) and the record is inserted into the 'projects' table with no issues.


Finally we have a view page, where we will call all employees assigned to a project, based on the query parameter, which would be the project ID. For example, if the previous project ID was 6, the following URL would be used:

From this page, I am able to save the staff list using the following variable assignment:

$project = "SELECT * FROM projects WHERE project = 6";
$employee_chosen = $project['project_employee']

If the 'staff' column only accepted one employee (for example, just one value of 4), the variable would have a value of one number:

$project['project_employee'] (4)

I would then be able to run a secondary query for employees as such:

$employee_chosen = $project['project_employee']; (4)
query2 = "SELECT * FROM employees WHERE employee_ID = $employee_chosen";

This would very easily bring back the one employee that was entered in the "staff" column. However, we are dealing with an array in this column value (2, 3, 5) and so I have queried the following statement:

$employee_list = $project['project_staff']; (2,3,5)
$query_employees = "SELECT * FROM employees WHERE employee_id IN ($employee_list)";

When I run this query, I receive only the first result from the employee ID 2 (as initially stated with the HTML form).

However, if I use phpMyAdmin to directly type in the three numbers as a string:

$query_employees = "SELECT * FROM employees WHERE employee_id IN (2,3,5)";

I receive all three employee records.

Just to ensure that the column ARRAY was in fact behaving as a STRING, I initiated a var_dump on the value:

echo var_dump($project['project_staff']);

After which I received the following information:

string(7) "4, 5, 6"

Does anyone have any ideas?

I am satisfied with the idea that I am able to query the value, as before I received several non-object and array errors.

Thanks in advance for any assistance you may be able to provide.

1条回答 默认 最新

相关推荐 更多相似问题