gpt4 book ai didi

php - 如何在查询字符串中的 post 变量中包含变量? -php

转载 作者:行者123 更新时间:2023-11-29 13:21:27 24 4
gpt4 key购买 nike

我有一张带有很多复选框的表格。这些复选框是通过循环生成的,并被赋予一个唯一的 ID。

$get_depts = mysql_query("select dept_id, dept_name from depts where bus_id = '{$business['bus_id']}'");
while(($department = mysql_fetch_assoc($get_depts)))
{
echo '<tr><td>'.$department['dept_name'].'</td>';
$req_fields = mysql_fetch_assoc(mysql_query("SELECT employees, department, customer, salespeople, acct_nbrs, categories, subcategories, case_ids, login_ids, phone, phext, cust_email, problem, resolution, source, priority FROM required_fields WHERE dept_id = '{$department['dept_id']}'"));
if($req_fields['employees'] == 'YES'){echo '<td><input type="checkbox" id="employees[]" name="employees[]" value="YES" checked></td>';}
else echo'<td><input type="checkbox" id="employees[]" name="employees[]" value="YES"></td>';
}

如您所见,复选框的 ID 和名称的一个示例是员工1、员工2 等。

提交后,我尝试循环回我的部门以再次获取唯一的 ID 号。然后我尝试更新我的表格。

$get_depts = mysql_query("select dept_id, dept_name from depts where bus_id = '{$business['bus_id']}'");
while(($department = mysql_fetch_assoc($get_depts)))
{
mysql_query("Update required_fields set employees = '{$_POST['employees'.$department['dept_id']]}' where dept_id = '{$department['dept_id']");
}

我显然没有对 post 变量使用正确的语法。如何正确使用 $_POST['employees'] + $department['dept_id']?

编辑

最终结果如下所示。例如,如果 $department['dept_id'] = 10,则 post 变量的名称将为 $_POST['employees10']

最佳答案

如果您对将它们直接串联在一起感到满意,您可以简单地使用:

$_POST['员工'] 。 $department['dept_id']

. 用于 PHP 中的串联。

例如:

$get_depts = mysql_query("select dept_id, dept_name from depts where bus_id = '{$business['bus_id']}'");
while(($department = mysql_fetch_assoc($get_depts)))
{
//
// I prefer to do my concat outside of the query!
//
$employees = $_POST['employees'] . $department['dept_id'];

//I assume this query isn't really what you're running. Pseudo-code, no?
mysql_query("Update required_fields set employees = '$employees' where dept_id = '{$department['dept_id']}");
}

关于php - 如何在查询字符串中的 post 变量中包含变量? -php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20791148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com