gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:22:50 25 4
gpt4 key购买 nike

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

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

@巴马尔

首先......

<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>

最佳答案

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

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

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

关于php - 如何在下拉列表中的用户选择和选择查询之间建立关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42343523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com