我有一张包含很多复选框的表格。复选框通过循环生成,并被赋予唯一的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和名称的一个例子是employees1,employees2等。
提交后,我尝试循环回到我的部门,再次获取唯一的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']?
修改 的
FINAL 结果如下所示。例如,如果$ department ['dept_id'] = 10,那么post变量的名称将是$ _POST ['employees10']
答案 0 :(得分:1)
如果您对将它们直接连接起来感到满意,可以使用:
$_POST['employees'] . $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']}");
}
答案 1 :(得分:0)
在第一个代码中,您需要为输入字段指定uniqe名称。您正在使用数组,但它只会保存一些自动生成的数字(0,1,2,3,4 ...)您应该将其更改为数据库中的部门ID,因此它看起来更像是这样:
$depid=$department['dept_id'];
if($req_fields['employees'] == 'YES'){echo "<input type='checkbox' id='employees[{$depid}]' name='employees[{$depid}]' value='YES' checked></td>";}
else echo "<td><input type='checkbox' id='employees[]' name='employees[]' value='YES'></td>";
}
您还需要使用双引号而不是单引号,因为只有这样您的字符串才会被变量解析。
然后在你的第二个代码中,Post部分看起来像这样:
$dep = $_POST['employees'][$department['dept_id'];
mysql_query("Update required_fields set employees = '$dep' where dept_id = '{$department['dept_id']");