我有一个带有一些字段的表单。地址就是其中之一。我用textarea标签作为地址。 现在的问题是,当我根据一个地址搜索某些内容时,它只适用于一行地址。我的意思是如果我在我的地址中输入换行符(段落格式),它不会显示结果。 所有其他搜索查询都运行得很好。这是我的代码,
地址下拉列表代码:
<input type="hidden" name="category_address" value="address"/>
<select name='criteria_address' style="width:100px;">
<option selected="selected"></option>
<?php
$order = "SELECT DISTINCT address FROM lh_clients ORDER BY clientname" or die (mysql_error());
$result = mysql_query($order);
while ($data = mysql_fetch_array($result)) {
echo ("<option>$data[address]</option>");
}
?>
</select>
搜索查询代码:
if (isset($_POST['criteria_address']))
{
$category_address = $_POST['category_address'];
$criteria_address = $_POST['criteria_address'];
$query = "SELECT * FROM lh_clients WHERE $category_address = '$criteria_address' ORDER BY clientname";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while ($data = mysql_fetch_array($result)) { ?>
<tr>
<td style="vertical-align:top;"><? echo $data['clientname']; ?></td>
<td style="vertical-align:top;"><? echo $data['clienttype']; ?></td>
<td style="vertical-align:top;"><? echo nl2br($data['address']); ?></td>
<td style="vertical-align:top;"><? echo $data['contacts']; ?></td>
<td style="vertical-align:top;"><? echo $data['sensitivity']; ?></td>
<td style="vertical-align:top;"><? echo $data['acountmanager']; ?></td>
<td style="vertical-align:top;"><? echo $data['responsibleexecutive']; ?></td>
<td></td>
</tr>
<?php
}
}
我认为搜索查询不适用于地址中的换行符。
答案 0 :(得分:0)
您可以尝试使用LIKE
运算符,如下所示:
WHERE $category_address LIKE '{$criteria_address}%'
这将找到所有以$ criteria address开头的记录。
答案 1 :(得分:0)
嗯...
$order = "SELECT DISTINCT address FROM lh_clients ORDER BY clientname" or die (mysql_error());
$result = mysql_query($order);
while ($data = mysql_fetch_array($result)) {
foreach (explode("\n", $data[address]) as $line) {
echo ("<option value='".htmlentities($line)."'>{$line}</option>");
}
}
尝试在文本列中使用LIKE进行搜索
$address = trim(html_entity_decode($criteria_address));
$query = "SELECT * FROM lh_clients WHERE $category_address like '%{$address}%' or $category_address like '{$address}%' or $category_address like '%{$address}' ORDER BY clientname";