dongruidian3064 2015-09-05 19:59
浏览 959
已采纳

如何将MySQL列值从数组转换为字符串

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>
    </select>
</form>

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.

HEREIN LIES THE PROBLEM.

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:

site.com/project/view/?project=6

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条回答 默认 最新

  • douzhan5262 2015-09-05 22:43
    关注

    I'm pretty sure from what you are saying that you are storing a string $employee_list that might be '2,3,4'. Then your IN ($employee_list) is really IN ('2,3,4') but what you really want is IN (2,3,4). There are various ways to get there but you could do

    $employee_list = implode(','(explode(',', $employee_list));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥100 某宝多次访问被拒绝,求解
  • ¥15 (标签-STM32|关键词-智能小车)