如何关联下拉列表和SQL查询

时间:2017-02-20 13:46:45

标签: php mysql wordpress select drop-down-menu

我有一个SQL查询,它根据4个下拉列表菜单中的用户选择从5个表中检索数据。

SQL查询工作正常并显示所需的数据...但是当我尝试使某些值与用户的选择相关时,查询不再起作用。

一些注释:

首先......

<td><select id="site_name"  name = "site_name">

                 <?php
                     $query_site_name =$wpdb->get_results ("select DISTINCT siteNAME  from site_info");
                      foreach($query_site_name as $site_name)
                      {
                       $site_name = (array)$site_name;
                       echo "<option value = '{".$site_name ['siteNAME']."}'>".  $site_name['siteNAME']."</option>";
                      } 
                 ?>

...第二

if(isset($_POST['site_name'])) 
          { 
           $site_name=$_POST['site_name'];
          }
          else { $site_name=""; }

我将$ site_name声明为全局变量

SQL查询:

 $query_submit =$wpdb->get_results ("select site_info.siteID,site_info.siteNAME ,site_info.equipmentTYPE,site_coordinates.latitude,site_coordinates.longitude,site_coordinates.height ,owner_info.ownerNAME,owner_info.ownerCONTACT,company_info.companyNAME,subcontractor_info.subcontractorCOMPANY,subcontractor_info.subcontractorNAME,subcontractor_info.subcontractorCONTACT from `site_info`
    LEFT JOIN `owner_info`
    on site_info.ownerID = owner_info.ownerID
    LEFT JOIN `company_info` 
    on site_info.companyID = company_info.companyID
    LEFT JOIN `subcontractor_info` 
    on site_info.subcontractorID = subcontractor_info.subcontractorID
    LEFT JOIN `site_coordinates` 
    on site_info.siteID=site_coordinates.siteID 
    ");

当我尝试将此行添加到查询时,它无效。

 where 
        site_info.siteNAME = ".$site_name."

其中$ site_name必须与下拉列表查询相关

下拉列表:

  <form method ="post" action ="" name="submit_form">
        <table width="30%">
            <tr>
               <td>Site Name</td>
               <td>Owner Name</td>
               <td>Company Name</td>
               <td>Subcontractor Name</td>
             </tr>
             <tr>
               <td><select id="site_name"  name = "site_name">

                 <?php
                     $query_site_name =$wpdb->get_results ("select DISTINCT siteNAME  from site_info");
                      foreach($query_site_name as $site_name)
                      {
                       $site_name = (array)$site_name;
                       echo "<option value = '{".$site_name ['siteNAME']."}'>".  $site_name['siteNAME']."</option>";
                      } 
                 ?>

                <!--create  dropdown list owner names-->
                </select></td>

                <td><select id="owner_name"  name ="owner_name">
                <?php
                      $query_owner_name =$wpdb->get_results ("select DISTINCT ownerNAME  from owner_info");
                      foreach($query_owner_name as $owner_name)
                      {
                         $owner_name = (array)$owner_name;
                         echo "<option value = '{".$owner_name ['ownerNAME']."}'>".  $owner_name['ownerNAME']."</option>";
                      } 
                  ?>
                </select></td>

                <!--create  dropdown list Company names-->
                </select></td>

                <td><select id="Company_name"  name ="Company_name">
                <?php 
                     $query_Company_name =$wpdb->get_results ("select DISTINCT companyNAME  from company_info");
                     foreach($query_Company_name as $Company_name)
                     {
                       $Company_name = (array)$Company_name;
                       echo "<option value = '{".$Company_name ['companyNAME']."}'>".  $Company_name['companyNAME']."</option>";
                     }  
                 ?>
                </select></td>

                <!--create  dropdown list Subcontractor names-->
                </select></td>

                <td><select id="Subcontractor_name"  name ="Subcontractor_name">
                <?php 
                    $query_Subcontractor_name =$wpdb->get_results ("select DISTINCT subcontractorNAME  from subcontractor_info");
                     foreach($query_Subcontractor_name as $Subcontractor_name)
                     {
                       $Subcontractor_name = (array)$Subcontractor_name;
                       echo "<option value = '{".$Subcontractor_name ['subcontractorNAME']."}'>".  $Subcontractor_name['subcontractorNAME']."</option>";
                      } 
                   ?>
                </select></td>
            <tr>
         <td></td>
         <td></td>
         <td></td>
         <td></td>
         <td>
            <input type ="submit" name="query_submit" value ="Search" />

        </td>
       </tr>

        </table>
    </form>

0 个答案:

没有答案