当只存在一个匹配时,MySQL返回多个条目(一个显示不正确的URL)

时间:2013-09-08 17:39:45

标签: php mysql

我有一个相当简单的数据库,只有一个表包含Texas Courts的联系信息。搜索表单允许用户选择该类型的法院,然后输入他们希望搜索的城市或县名称(单独的字段)。

代码:

$sql = mysql_query ("SELECT * FROM COURTS
                      WHERE Type = '$_POST[Type]' AND City LIKE '$_POST[City]' 
                      OR Type = '$_POST[Type]' AND County LIKE '$_POST[County]'
                      ORDER BY County, City")

or die(mysql_error()); ?>

<table id="customers">
<?php while($rows = mysql_fetch_array($sql)): ?>
<tr class="alt">
<td><?php echo $rows[Court]; ?>&nbsp<?php echo $rows[Type]; ?><br></td>
<td><?php echo $rows[City]; ?>,&nbsp<?php echo $rows[State]; ?><br></td>
<td><?php echo "<a href=$rows[URL]>Court Info</a>"; ?><br></td>
</tr>
<?php endwhile; ?>
</table>

当搜索名称与县名不同的城市时(例如,Boerne,Kendall County),当表中只有一个存在时,MySQL会返回该城市的两个结果。第一个返回具有指向当前搜索结果页面的超链接,第二个链接指向正确的信息页面。如果一个城市与该县共享其名称(例如Bandera,Bandera County),则返回是正确的 - 一个具有正确URL的列表。

我尝试过分组,排序和哭泣,但没有任何作用。

编辑:

添加paranthesis仍会产生同样的问题...

$sql = mysql_query ("SELECT * FROM COURTS
                      WHERE (Type = '$_POST[Type]' AND City LIKE '$_POST[City]') 
                      OR (Type = '$_POST[Type]' AND County LIKE '$_POST[County]')
                      ORDER BY County, City")

编辑:表格结构......

+--------------+--------------+----------+-----------+---------+-------+-------+---------------+--------------+----------+--------------+
|     Type     |    Court     |  County  |  Street   |  City   | State |  Zip  |     Email     |   Website    |  Phone   |     URL      |
+--------------+--------------+----------+-----------+---------+-------+-------+---------------+--------------+----------+--------------+
| Justice of.. | Precinct 1.. | Anderson | P O Box.. | Elkhart | TX    | 75839 | gthomas@co... | http://www.. | 903-76.. | http://www.. |
+--------------+--------------+----------+-----------+---------+-------+-------+---------------+--------------+----------+--------------+

当城市名称与其郡名称不匹配时的结果:

+-------------------+-------------+------------+
| Boerne Municipal  | Boerne, TX  | Court Info | <<Court info links to the wrong URL (current page)
+-------------------+-------------+------------+
| Boerne Municipal  | Boerne, TX  | Court Info | <<Court Info links to correct URL
+-------------------+-------------+------------+

上述结果适用于Boerne市,该市仅有一个市法院条目。

1 个答案:

答案 0 :(得分:0)

你需要括号......

$sql = mysql_query ("SELECT * FROM COURTS
                      WHERE (Type = '$_POST[Type]' AND City LIKE '$_POST[City]') 
                      OR (Type = '$_POST[Type]' AND County LIKE '$_POST[County]')
                      ORDER BY County, City")