日期:2014-05-17  浏览次数:20589 次

如何实现多个查找框内只需填写至少一个查找内容即可进行查找
apply.html
<html>
<head>
  <title>A new Application for a classroom</title>
</head>

<body>
  <h1>CRMS - New classroom Entry</h1>

  <form action="2.php" method="post">
  <table border="0">
  <tr>
  <td>Classroom ID</td>
  <td><input type="text" name="Cno" maxlength="13" size="13"></td>
  </tr>
  <tr>
  <td>Course ID</td>
  <td> <input type="text" name="CID" maxlength="30" size="30"></td>
  </tr>
  <tr>
  <td colspan="2"><input type="submit" value="Register"></td>
  </tr>
  </table>
  </form>
</body>
</html>

2.php

<html>
<head>
  <title>A new Application for a classroom</title>
</head>
<body>
<h1>CRMS - New classroom Entry Results</h1>
<?php
  // create short variable names
  $Cno=$_POST['Cno'];
  $CID=$_POST['CID'];

  if (!$Cno) {$Cno = "%";}
  if (!$CID) {$CID = "%";}

  if (!get_magic_quotes_gpc()) {
  $Cno = addslashes($Cno);
  $CID = addslashes($CID);
  }

$con = mysql_connect("localhost","root","");
//通过服务器locahost建立连接,用户名为root,无密码

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("crms", $con);

$search_classroom = "SELECT * FROM use2 
  WHERE Cno LIKE '{$Cno}' AND CID LIKE $CID";
$result = mysql_query($search_classroom, $con);

while($row = mysql_fetch_array($result))
  {
  echo $row['Cno'];
  echo $row['CID'];
  echo "<br />";
  }
mysql_close($con)
?>
</body>
</html>

为什么这样不能实现当Classroom ID 或者Course ID没有填写时,任然能够成功搜索。我已经写了 if (!$Cno) {$Cno = "%";}
  if (!$CID) {$CID = "%";}如果没有填就用通配符,而且查询时也用了LIKE(如果查询时LIKE全部换为=则成功)。为什么?在线等!

------解决方案--------------------
因为你用like了。去查下mysql手册。