douzhuang6321 2013-12-26 20:57
浏览 61
已采纳

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

I have a table with a lot of checkboxes. The checkboxes are being produced via a loop, and are given a unique 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>';
}

As you can see, one such example of the id and name of a checkbox is employees1, employees2, etc.

After submit, I try and loop back through my departments to pick up the unique id numbers again. Then I try and update my table.

$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']");
}

I obviously am not using the correct syntax for the post variable. How can I correctly use $_POST['employees'] + $department['dept_id']?

EDIT

The FINAL result would look like this. If $department['dept_id'] = 10 for example, then the name of the post variable would be $_POST['employees10']

  • 写回答

2条回答 默认 最新

  • duanji2002 2013-12-26 21:00
    关注

    If you're happy with having them concatenated straight across, you can simply use:

    $_POST['employees'] . $department['dept_id']

    The . is used for concatenation in PHP.

    For example:

    $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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统