-1

如果有人可以为我提供正确的代码,那就太好了。我要做的是<hr />在仅当从我的数据库中提取多个结果时才在回显的信息之后添加一个。如果有人可以帮助我,这是代码。谢谢。

<html>
<script>
function goBack()
  {
  window.history.back()
  }
</script>
<body>
<div style="width: 875px; margin-left: 30px; margin-right: auto;"><img         src="searchresults.png" alt="" title="Search Results"  alt="" /></p>
<?php


$term = $_POST['term'];

$sql = mysql_query("SELECT * FROM store_location where store_name like '%$term%' or     address like '%$term%' or city like '%$term%' or state like '%$term%' or zip like     '%$term%' or phone like '%$term%' or fax like '%$term%' or email like '%$term%' or url     like '%$term%' ");

    if( mysql_num_rows($sql) == 0) echo "<p>No TeachPro Store(s) in your area.</p>";

   while ($row = mysql_fetch_array($sql)){

echo 'Store Name: '.$row['store_name'];
echo '<br/> Address: '.$row['address'];
echo '<br/> City: '.$row['city'];
echo '<br/> State: '.$row['state'];
echo '<br/> Zip: '.$row['zip'];
echo '<br/> Phone: '.$row['phone'];
echo '<br/> Fax: '.$row['fax'];
echo '<br/> Email: <a href="mailto:'.$row['email'].'">'.$row['email'].'</a>';
echo '<br/> URL: <a href="'.$row['url'].'">'.$row['url'].'</a>';
echo '<br/><br/>';
}
?>
</div>
<input type="button" value="Back" onclick="goBack()">
</body>
</html>
4

1 回答 1

1

while只需将您的循环包装在一个else案例中并<hr>在那里输出。<p>如果没有找到行,您已经有适当的逻辑来输出 a ,并且您可以扩展它。

if( mysql_num_rows($sql) == 0) {
  echo "<p>No TeachPro Store(s) in your area.</p>";
}
// Instead of relying on an empty fetch to output nothing, put it in an else {}
else {
  while ($row = mysql_fetch_array($sql)){
    echo 'Store Name: '.$row['store_name'];
    echo '<br/> Address: '.$row['address'];
    echo '<br/> City: '.$row['city'];
    echo '<br/> State: '.$row['state'];
    echo '<br/> Zip: '.$row['zip'];
    echo '<br/> Phone: '.$row['phone'];
    echo '<br/> Fax: '.$row['fax'];
    echo '<br/> Email: <a href="mailto:'.$row['email'].'">'.$row['email'].'</a>';
    echo '<br/> URL: <a href="'.$row['url'].'">'.$row['url'].'</a>';
    echo '<br/><br/>';
  }
  // And your <hr /> and whatever else you need...
  echo "<hr />";
}

只是关于 HTML 输出的一个旁注 - 确保将这些值包装起来htmlspecialchars()以正确转义为 HTML,以避免在它们包含 HTML 特殊字符时出现问题< > &(如果这是用户输入,则可能防止 XSS!)

// Ex:
echo 'Store Name: '.htmlspecialchars($row['store_name']);

更紧迫的是使用mysql_real_escape_string().

// At a minimum:
$term = mysql_real_escape_string($_POST['term']);

从长远来看,考虑切换到支持预处理语句的 API,如 MySQLi 或 PDO。

于 2012-12-06T21:40:43.990 回答