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

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 返回两个结果。第一个返回有一个指向当前搜索结果页面的超链接,第二个返回指向正确信息页面的链接。如果一个城市与县共享其名称(例如班德拉、班德拉县),则返回是正确的 - 一个具有正确 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 市。

4

1 回答 1

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")
于 2013-09-08T17:41:47.177 回答