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


MySQL returning multiple entries when only one match exists (one display incorrect URL)

我有一个相当简单的数据库,只有一个表格,其中包含德克萨斯州法院的联系信息。搜索表单允许用户选择该类型的法院,然后输入他们想要搜索的城市或县名称(单独的字段)。

代码:

$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,肯德尔县),当表中仅存在一个结果时,MySQL 会返回两个城市结果。第一个返回具有指向当前搜索结果页面的超链接,第二个返回具有指向正确信息页面的链接。如果一个城市与该县共享其名称(例如。班德拉,班德拉县)返回是正确的 - 一个具有正确 URL 的列表。

我尝试过分组、排序和哭泣,但没有任何效果。

编辑:

添加偏执仍然会产生同样的问题......

$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市,该市的数据库中只有一个市法院条目。

你需要偏执...

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