dowm41315 2017-03-10 13:55
浏览 45
已采纳

在PHP中从列表框构建查询

I have a page where I am wanting to allow a user to select fields from a select that exist in a table, then display the contents of those fields on screen. I have set-up the select like so

<select name="queryfields" size="12" multiple="multiple" tabindex="1">
<option value="firstname">firstname</option>
<option value="lastname">lastname</option>
<option value="address">address</option>
<option value="phone">phone</option>

And I know to discover what options were selected I can use this:

<?php
    header("Content-Type: text/plain");

    foreach ($_GET['queryfields'] as $selectedOption)
        echo $selectedOption."
";
?>

And that gives me an array of the fields selected. However, how do I then parse the array to generate my full query? For example, let's say that firstname, lastname were selected. I would then want to build my query like this:

Select firstname, lastname from employeedata

Unknown to me, is how to get the data from the array into a select statent like my above code snippet.

  • 写回答

2条回答 默认 最新

  • douya2982 2017-03-10 14:21
    关注

    Try This:

    $sql = '';
    $selected_fields = array();   
    foreach ($_GET['queryfields'] as $selectedOption){
        //echo $selectedOption."
    ";
        $selected_fields[] = $selectedOption;
    }
    if(!empty($selected_fields)){
        $fields = implode(',', $selected_fields);
        $sql = 'SELECT '.$fields.' from employeedata'; 
    }
    

    //print query if it is not empty

    if(!empty($sql)){
        echo $sql;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码