我有一个包含SQL查询的PHP代码,用户从下拉列表中选择,并根据其选择SQL查询检索数据。
问题在于,当我尝试将所选数据与数据库中的现有值进行比较时,查询无法正常工作。我试图在查询之前和之后显示变量的值,它与用户选择相同。所以我确信问题出在 SQL QUERY 里面我比较i.siteNAME = '".$site_name."'
如何解决这个问题我已经坚持使用这段代码5天了,还在计算中。
这是代码的一部分:
<?php
/*
Template Name: search info
*/
get_header();
?>
<?php
// code for submit button action
global $wpdb, $site_name;
//variables that handle the retrieved data from mysql database based on the ID of the variable in HTML (select)
if(isset($_POST['query_submit']))
{
if(isset($_POST['site_name']))
{
$site_name=$_POST['site_name'];
}
else { $site_name=""; }
if(isset($_POST['owner_name']))
{
$owner_name=$_POST['owner_name'];
}
else { $owner_name=""; }
if(isset($_POST['Company_name']))
{
$company_name=$_POST['Company_name'];
}
else { $company_name=""; }
if(isset($_POST['Subcontractor_name']))
{
$Subcontractor_name=$_POST['Subcontractor_name'];
}
else { $Subcontractor_name="";}
var_dump($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."'
");
var_dump($_POST['site_name']);
echo "<br>";
echo "<br>";
echo $site_name;
echo $owner_name;
echo $company_name;
echo $Subcontractor_name;
foreach ($query_submit as $obj) {
echo "query is working";
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>";
}
?>
<table width="30%" >
<tr>
<td>Site Name</td>
<td>Owner Name</td>
<td>Company Name</td>
<td>Subcontractor Name</td>
<td>Site ID</td>
<td>Equipment Type</td>
<td> Lattitude</td>
<td>Longitude </td>
<td> Height</td>
<td> Owner Contact</td>
<td> Sub Contact</td>
<td> Sub company Name</td>
</tr>
<tr>
<?php }
?>
<!-- the below part of code work as it should --!>
<!--create dropdown list site names-->
<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
global $owner_name;
$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
global $Company_name;
$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
global $Subcontractor_name;
$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>
<?php
get_footer();
?>
答案 0 :(得分:-1)
我解决了我的问题,查询中没有任何错误,问题出在 foreach 循环内的迭代过程中。
我将查询提取为数组,这是必须提取的错误对象 我改变的路线是:
echo "<option value = '{".$owner_name ['ownerNAME']."}'>". $owner_name['ownerNAME']."</option>";
它变成了
echo "<option value = '".$owner_name->ownerNAME."'>". $owner_name->ownerNAME."</option>";