如何将用户使用PHP的输入与SQL查询中的MySQL数据库相关联,以便检索所需的数据。
我的代码中的错误在哪里
if(isset($_POST['site_name']))
{
$site_name=$_POST['site_name'];
}
else { $site_name=""; }
$query_submit =$wpdb->get_results ("
select i.siteID
, i.siteNAME
, i.equipmentTYPE
, c.latitude
, c.longitude
, c.height
, o.ownerNAME
, o.ownerCONTACT
, x.companyNAME
, y.subcontractorCOMPANY
, y.subcontractorNAME
, y.subcontractorCONTACT
from site_info i
LEFT
JOIN owner_info o
on i.ownerID = o.ownerID
LEFT
JOIN company_info x
on i.companyID = x.companyID
LEFT
JOIN subcontractor_info y
on i.subcontractorID = y.subcontractorID
LEFT JOIN `site_coordinates` c
on i.siteID=c.siteID
where
i.siteNAME = '".$site_name."'
AND
o.ownerNAME = '".$owner_name." '
AND
x.companyNAME = '".$company_name."'
");
这是下拉列表代码:
<?php
global $site_name;
$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>";
}
?>
此查询如果我替换 where statement 中的1个变量,它可以正常工作 但是使用这些变量,查询不起作用。
我在SQL查询之后和 foreach循环之前使用了var_dump并返回了所选的值
var_dump($_POST['site_name']);