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

如何将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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作