表单页代码:<html>
<head>catalog search</head>
<body>
<h1>catalog search</h1>
<form action="results.php" method="post">
choose search type:<br/>
<select name="searchtype">
<option value="author">author</option>
<option value="title">title</option>
<option value="isbn">isbn</option></select>
<br/>
enter search term:<br/>
<input name="searchterm" type="text" size="40"/>
<br/>
<input type="submit" name="submint" value="search"/>
</form>
</body></html><html>
<body>
<h1>search results</h1>
<?php
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);
if(!$searchtype||!$searchterm){
echo"请输入值";
exit;}
if(!get_magic_quotes_gpc())
{
$searchtype=addslashes($searchtype);
$searchterm=addslashes($searchterm);
}
@ $db=new mysqli('localhost','root','root','books');
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}$query="select * from books where".$searchtype."like '%".$searchterm."%'";
$result=$db->query($query);
$num_results=$result->num_rows;
echo "<p>Number of books found:".$num_results."</p>";
for ($i=0;$i<$num_results;$i++)
{
$row=$result->fetch_assoc();
echo"<p><strong>".($i+1)."title:";
echo htmlspecialchars(stripslashes($row['title']));
echo "</strong><br/>author:";
echo stripslashes($row['author']);
echo"<br/>isbn:";
echo stripslashes($row['isbn']);
echo"<br/>price:";
echo stripslashes($row['price']);
echo"</p>";
}
?>
</body>
</html>输出结果:search results
Number of books found:为什么查询不出结果
<head>catalog search</head>
<body>
<h1>catalog search</h1>
<form action="results.php" method="post">
choose search type:<br/>
<select name="searchtype">
<option value="author">author</option>
<option value="title">title</option>
<option value="isbn">isbn</option></select>
<br/>
enter search term:<br/>
<input name="searchterm" type="text" size="40"/>
<br/>
<input type="submit" name="submint" value="search"/>
</form>
</body></html><html>
<body>
<h1>search results</h1>
<?php
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);
if(!$searchtype||!$searchterm){
echo"请输入值";
exit;}
if(!get_magic_quotes_gpc())
{
$searchtype=addslashes($searchtype);
$searchterm=addslashes($searchterm);
}
@ $db=new mysqli('localhost','root','root','books');
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}$query="select * from books where".$searchtype."like '%".$searchterm."%'";
$result=$db->query($query);
$num_results=$result->num_rows;
echo "<p>Number of books found:".$num_results."</p>";
for ($i=0;$i<$num_results;$i++)
{
$row=$result->fetch_assoc();
echo"<p><strong>".($i+1)."title:";
echo htmlspecialchars(stripslashes($row['title']));
echo "</strong><br/>author:";
echo stripslashes($row['author']);
echo"<br/>isbn:";
echo stripslashes($row['isbn']);
echo"<br/>price:";
echo stripslashes($row['price']);
echo"</p>";
}
?>
</body>
</html>输出结果:search results
Number of books found:为什么查询不出结果
解决方案 »
- unexpected T_VARIABLE, expecting ',' or ';' php
- [求助]关于Apache2.2的VirtualHost问题
- 同时向两个页面提交!可以吗??
- mai()函数的奇怪问题不知道大家碰到过没有
- MySQL做后台数据库有什么好处?
- 怎么通过在页面写代码来解决页面session过期的问题呀?
- 请帮忙!!急!!!
- 谁有带数据库支持的bbs的代码啊?
- 这个如何实现???急 (在线等待)
- 请高手看看下面一段程序,关于认证的!怎么老是调试不出?
- 新人问个很菜的问题是zend studio 调试出问题
- 想做一下PHP调用java webservice接口的例子,但是...
1、确认 $searchtype 是正确的字段名
2、$query="select * from books where".$searchtype."like '%".$searchterm."%'";
中 like 前少了个空格
写成这样是不是清爽些?
$query = "select * from books where $searchtype like '%$searchterm%'";
改成这样$query = "select * from books where $searchtype like '%$searchterm%'";就可以了 可是为什么呢、
$query="select * from books where".$searchtype."like '%".$searchterm."%'";
$query = "select * from books where $searchtype like '%$searchterm%'";
这两句不是等价的吗
echo $query;就知道是否等价了。 红字部分连在一起了还对吗?
$query = "select * from books where $searchtype like '%$searchterm%'";
这两个sql是有区别的。
""里面的都是字符串,然后你没有空格直接写.$searchtype。你自己想想结果一样吗?