如何在下拉列表和选择查询之间建立用户选择之间的关系

时间:2017-02-20 11:36:27

标签: php mysql wordpress

我有PHP脚本,允许用户从4个下拉列表中选择,其中这些下拉列表包含从MySQL数据库中检索的值。

当我在phpMyAdmin控制台上尝试SQL查询时,它工作正常。当我尝试使用PHP脚本时,它无法正常工作,也无法检索到任何内容。

@ barmar

首先......

<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 
    ");

foreach ($query_submit as $obj) {
             echo $site_name;
    echo "<table width='30%' ";
echo     "<tr>";
echo         "<td>".$obj->siteNAME."</td>";
echo         "<td>".$obj->ownerNAME."</td>";
echo         "<td>".$obj->companyNAME."</td>";
echo         "<td>".$obj->subcontractorNAME."</td>";
echo         "<td>".$obj->siteID."</td>";
echo         "<td>".$obj->equipmentTYPE."</td>";
echo         "<td>".$obj->latitude."</td>";
echo         "<td>".$obj->longitude."</td>";
echo         "<td>".$obj->height."</td>";
echo         "<td>".$obj->ownerCONTACT."</td>";
echo         "<td>".$obj->subcontractorCONTACT."</td>";
echo         "<td>".$obj->subcontractorCOMPANY."</td>";
echo     "</tr>";
echo "</table>";
    } 


 ?>  

新问题是当我试图指定用户选择查询时查询停止工作。

我将添加以下内容:

 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>

1 个答案:

答案 0 :(得分:0)

您需要在网站名称周围加引号,因为它是一个字符串:

where 
    site_info.siteNAME = '".$site_name."'

但如果您使用预准备语句而不是将变量替换为SQL会更好,请参阅wpdb::prepare()