doumingchen3628 2015-08-25 04:02
浏览 73
已采纳

SQL查询值从$ _GET更改

How can I modify MySQL query from Php, so If I selected ALL, there will not be Where statement, If I select some user login, there will show only this user records? I need that for example If I selected <option value="All">ALL</option> Query will be without WHERE statement, But if, for example I selected <option value="admin">Admin</option> there will be in this query Where tsales_funnel.Darijuma_vaditajs = 'admin'

SQL Statement:

$sql = "Select
  tsales_funnel.ID As ID,
  wp_users.display_name As Darijuma_vaditajs,
  tcportal_starpnieks.Cp_Name As Starpnieks,
  tcportal_stucture.Cp_Name As OWCA,
  n_products.Product_Nos As Produkts,
  tsales_funnel_mrecord.Deadline As Deadline,
  tsales_funnel_clients.Reg_nr As Klienta_Regnr,
  tfirmas_reg.name_in_quotes As Klients,
  tsales_funnel_mrecord.ID As Prod_ID,
  tsales_funnel.Statuss As Statuss
From
  tsales_funnel Left Join
  tsales_funnel_mrecord On tsales_funnel_mrecord.Funnel_ID = tsales_funnel.ID
  Left Join
  tcportal_starpnieks On tcportal_starpnieks.Cp_code = tsales_funnel.Starpnieks
  Left Join
  tcportal_stucture On tcportal_stucture.Cp_code = tsales_funnel.OWCA Left Join
  tsales_funnel_clients On tsales_funnel_clients.Funnel_ID = tsales_funnel.ID
  Left Join
  tfirmas_reg On tfirmas_reg.regcode = tsales_funnel_clients.Reg_nr Left Join
  n_products On tsales_funnel_mrecord.Product_type = n_products.Product_Code
  Left Join
  wp_users On tsales_funnel.Darijuma_vaditajs = wp_users.user_login ".$sqlselecteduser."
Order By
  Deadline";

GET method from form:

<?php
if(isset($_GET['selecteduser'])){
    $selecteduser = esc_sql($_GET['selecteduser']);
} else {
    $selecteduser = $_SESSION['selecteduser'];
}
if($selecteduser == 'all') {
    $sqlselecteduser = '';
} else {
    $sqlselecteduser = " Where tsales_funnel.Darijuma_vaditajs = $selecteduser ";
}

?>

FORM TO SEND GET REQUEST TO THIS PAGE:

<form action="" method="GET" id="selectdo">
<table border="1">
    <tr>
        <td>
            <label for="selector1">Darījuma vadītājs:</label>
        </td>
        <td>
            <select id="selector1" name="selecteduser">
            <option value="all">-ALL-</option>
            <?php 
                $sql = "SELECT display_name, user_login FROM wp_users";
                $results = $wpdb->get_results($sql);  // return an object, not ARRAY_N
            if ($results) {
                foreach ($results as $row) {
                    echo "<option value = '".$row->user_login."'>".$row->display_name."</option>"; 
            }}
                echo "</select></br>";                          
            ?>
        </td>
        <td><input type="submit" value="ok" /></td>
    </tr>
</table>
</form>

After that data displays into table.

展开全部

  • 写回答

2条回答 默认 最新

  • dpo69086 2015-08-25 04:58
    关注

    I'd prefer it this way

    $sql = "Select
        tsales_funnel.ID As ID,
        wp_users.display_name As Darijuma_vaditajs,
        tcportal_starpnieks.Cp_Name As Starpnieks,
        tcportal_stucture.Cp_Name As OWCA,
        n_products.Product_Nos As Produkts,
        tsales_funnel_mrecord.Deadline As Deadline,
        tsales_funnel_clients.Reg_nr As Klienta_Regnr,
        tfirmas_reg.name_in_quotes As Klients,
        tsales_funnel_mrecord.ID As Prod_ID,
        tsales_funnel.Statuss As Statuss
    From
        tsales_funnel Left Join
        tsales_funnel_mrecord On tsales_funnel_mrecord.Funnel_ID = tsales_funnel.ID
        Left Join
        tcportal_starpnieks On tcportal_starpnieks.Cp_code = tsales_funnel.Starpnieks
        Left Join
        tcportal_stucture On tcportal_stucture.Cp_code = tsales_funnel.OWCA Left Join
        tsales_funnel_clients On tsales_funnel_clients.Funnel_ID = tsales_funnel.ID
        Left Join
        tfirmas_reg On tfirmas_reg.regcode = tsales_funnel_clients.Reg_nr Left Join
        n_products On tsales_funnel_mrecord.Product_type = n_products.Product_Code
        Left Join
        wp_users On tsales_funnel.Darijuma_vaditajs = wp_users.user_login 
    WHERE
        IF('".$selecteduser."' = 'all', 1=1, tsales_funnel.Darijuma_vaditajs = '".$selecteduser."')
    Order By
        Deadline";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码