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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值