PHP中我的MYSQL模糊查询语句到底错在哪里?请高手指教! 50
<?phprequire"head.php";echo"<tablewidth=\"992\"border=\"0\"align=\"center\"cellpaddin...
<?php
require "head.php";
echo"<table width=\"992\" border=\"0\" align=\"center\" cellpadding=\"1\" cellspacing=\"1\" bordercolor=\"#EBF2FA\">";
echo"<tr>";
echo"<td height=\"24\" bgcolor=\"#C7DCF1\"><span class=\"STYLE18\">下列是与您查询的内容有关的帖子:</span></td>";
echo"</tr>";
echo"</table><br>";
$stt=$_POST[textfield];
$sql1="select * from $b_posts where title like '%$stt%'";
$result1=mysql_query($sql1,$link) or die(mysql_error());
$nums=mysql_num_rows($result1);
if($nums==0)
{
echo"<div align=\"center\"><span class=\"STYLE18\">没有您想查找的内容!</span></div>";
}
else
{
$p_count=ceil($nums/35); //p_cont记录了总页数
if ($_GET["page"]==0 and !$_GET["page"] and $_GET["page"]>$p_count)
$page=1;
else
$page=$_GET[page];
$s=($page-1)*35+1;
$s=$s-1;
$sql="select * from $b_posts where title like '%$stt%' order by rtime desc limit $s, 35";
$result=mysql_query($sql,$link) or die(mysql_error());
while($rows=mysql_fetch_array($result))
{
echo"<table width=\"992\" border=\"1\" align=\"center\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#EBF2FA\">";
echo"<tr>";
echo"<td height=\"24\" bgcolor=\"#C7DCF1\"><span class=\"STYLE18\"><a href=\"look.php?id=$rows[id]\" target=\"_blank\">".$rows[title]."</a></span></td>";
echo"</tr>";
echo"</table>";
}
echo "<center>";
$prev_page=$page-1;
$next_page=$page+1;
if ($page<=1){
echo "<span class=\"STYLE18\">第一页 |</span> ";
}
else{
echo "<a href='$_SERVER[PHP_SELF]?id=$_GET[id]&page=1'>第一页</a> | ";
}
if ($prev_page<1){
echo "<span class=\"STYLE18\">上一页 | </span>";
}
else{
echo "<a href='$_SERVER[PHP_SELF]?id=$_GET[id]&page=$prev_page'>上一页</a> | ";
}
if ($next_page>$p_count){
echo "<span class=\"STYLE18\">下一页 |</span> ";
}
else{
echo "<a href='$_SERVER[PHP_SELF]?id=$_GET[id]&page=$next_page'>下一页</a> | ";
}
if ($page>=$p_count){
echo "<span class=\"STYLE18\">最后一页</span>";
}
else{
echo "<a href='$_SERVER[PHP_SELF]?id=$_GET[id]&page=$p_count'>最后一页</a>";
}
echo"<span class=\"STYLE18\"> | 共".$p_count."页</span><p>\n";
echo"<form name=\"form1\" method=\"get\" action=\"$PATH_INFO\">";
echo"<span class=\"STYLE18\">输入想要跳转的页数: </span>";
echo"<input name=\"page\" type=\"text\" size=\"3\" maxlength=\"3\"> ";
echo"<input type=\"submit\" name=\"Submit\" value=\"转到页面\">";
echo"</form>";
echo "</center>";
}
require "bottom.php";
?> 展开
require "head.php";
echo"<table width=\"992\" border=\"0\" align=\"center\" cellpadding=\"1\" cellspacing=\"1\" bordercolor=\"#EBF2FA\">";
echo"<tr>";
echo"<td height=\"24\" bgcolor=\"#C7DCF1\"><span class=\"STYLE18\">下列是与您查询的内容有关的帖子:</span></td>";
echo"</tr>";
echo"</table><br>";
$stt=$_POST[textfield];
$sql1="select * from $b_posts where title like '%$stt%'";
$result1=mysql_query($sql1,$link) or die(mysql_error());
$nums=mysql_num_rows($result1);
if($nums==0)
{
echo"<div align=\"center\"><span class=\"STYLE18\">没有您想查找的内容!</span></div>";
}
else
{
$p_count=ceil($nums/35); //p_cont记录了总页数
if ($_GET["page"]==0 and !$_GET["page"] and $_GET["page"]>$p_count)
$page=1;
else
$page=$_GET[page];
$s=($page-1)*35+1;
$s=$s-1;
$sql="select * from $b_posts where title like '%$stt%' order by rtime desc limit $s, 35";
$result=mysql_query($sql,$link) or die(mysql_error());
while($rows=mysql_fetch_array($result))
{
echo"<table width=\"992\" border=\"1\" align=\"center\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#EBF2FA\">";
echo"<tr>";
echo"<td height=\"24\" bgcolor=\"#C7DCF1\"><span class=\"STYLE18\"><a href=\"look.php?id=$rows[id]\" target=\"_blank\">".$rows[title]."</a></span></td>";
echo"</tr>";
echo"</table>";
}
echo "<center>";
$prev_page=$page-1;
$next_page=$page+1;
if ($page<=1){
echo "<span class=\"STYLE18\">第一页 |</span> ";
}
else{
echo "<a href='$_SERVER[PHP_SELF]?id=$_GET[id]&page=1'>第一页</a> | ";
}
if ($prev_page<1){
echo "<span class=\"STYLE18\">上一页 | </span>";
}
else{
echo "<a href='$_SERVER[PHP_SELF]?id=$_GET[id]&page=$prev_page'>上一页</a> | ";
}
if ($next_page>$p_count){
echo "<span class=\"STYLE18\">下一页 |</span> ";
}
else{
echo "<a href='$_SERVER[PHP_SELF]?id=$_GET[id]&page=$next_page'>下一页</a> | ";
}
if ($page>=$p_count){
echo "<span class=\"STYLE18\">最后一页</span>";
}
else{
echo "<a href='$_SERVER[PHP_SELF]?id=$_GET[id]&page=$p_count'>最后一页</a>";
}
echo"<span class=\"STYLE18\"> | 共".$p_count."页</span><p>\n";
echo"<form name=\"form1\" method=\"get\" action=\"$PATH_INFO\">";
echo"<span class=\"STYLE18\">输入想要跳转的页数: </span>";
echo"<input name=\"page\" type=\"text\" size=\"3\" maxlength=\"3\"> ";
echo"<input type=\"submit\" name=\"Submit\" value=\"转到页面\">";
echo"</form>";
echo "</center>";
}
require "bottom.php";
?> 展开
2个回答
展开全部
格式写法上很多都不规范
你最好对$stt进行tirm处理 去掉首尾的空格
$b_posts应该是个表名 以变量的形式引入不太妥当
下面的例子是我学PHP第5天写的 你参考下 不过千万不要学这种写法 仅适合初学者
<?php
require_once("./include/config.php");
require_once("./include/usercheck.php");
if (!empty($_GET["cs_Id"]))
{
$cs_Id = $_GET["cs_Id"];
}
elseif (!empty($_POST["cs_Id"]))
{
$cs_Id = $_POST["cs_Id"];
}
else
{
$cs_Id = 1;
}
$keyword = "";
if (!empty($_POST["keyword"]))
{
$keyword = $_POST["keyword"];
}
elseif (!empty($_GET["keyword"])){
$keyword = $_GET["keyword"];
}
else
{
$keyword = "";
}
if(isset($cs_Id))
{
// $sql="select * from tbarticle where cs_Id=".$cs_Id."";
// //echo $sql;
// $res=mysql_query($sql);
$wheresql = "";
if (!empty($keyword))
{
$wheresql = " and ae_Title like '%" .$keyword. "%'";
}
$sql1="select count(*) as amount from tbarticle where cs_Id=".$cs_Id.$wheresql." ";
//echo $sql1;
$result=mysql_query($sql1);
//获取总数据量
$row=mysql_fetch_array($result);
$amount=$row['amount'];
//exit;
//获取当前页数
if(isset($_GET['page']))
{
$page=intval($_GET['page']);
}
else
{
$page=1;
}
//每页数量
$page_size=2;
//计算总共有多少页
if($amount==0)
{
echo"没有任何数据";
exit;
}
if($amount < $page_size)
{
$page_count=1;
}
//if($amount%$page_size){//拿总数据除以每页的总数,如果有余数,那么总页数等于商+1
$page_count=ceil($amount/$page_size);
//翻页链接
$page_string='';
if ($page==1)
$page_string='第一页|<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.($page+1).'&keyword='.$keyword.'>下一页</a>|';
elseif(($page==$page_count||($page_count==0)))
$page_string='<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.($page-1).'&keyword='.$keyword.'>上一页</a>|<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.$page_count.'&keyword='.$keyword.'>尾页</a>';
elseif(($page>1)&&($page<$page_count))
$page_string='<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.($page-1).'&keyword='.$keyword.'>上一页</a>|<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.($page+1).'&keyword='.$keyword.'>下一页</a>|';
//||($page_count==0)
//获取数据
$a=($page-1)*$page_size;
$b=$page_size;
$query_s="select * from tbarticle where cs_Id=".$cs_Id.$wheresql." order by ae_Id limit $a,$b";
$result_s=mysql_query($query_s);
//echo $query_s;
//$rows=mysql_fetch_assoc($res);
echo"<table border=1>";
while($rows=mysql_fetch_array($result_s))
{
$rows["ae_Date"]= date("Y-m-j H:i:s",$rows["ae_Date"]);
echo "<tr>";
echo "<td align='left' valign='middle'><a href='articlereadshow.php?ae_Id=".$rows["ae_Id"]."' target=_blank><font>".$rows["ae_Title"]."[".$rows["ae_Date"]."]</font></a>  ";
echo "</tr>";
}
echo "</table>";
echo '一共有'.$page_count.'页<p>';
echo '当前为第'.$page.'页<p>';
echo $page_string;
}
//echo "test";
//echo $keyword;
//print_r($_POST);
?>
<form name=xxx action="classcontentshow.php" method="post">
<input type=text name=keyword value="<? echo $keyword;?>">
<input type=hidden name=cs_Id value="<? echo $cs_Id;?>">
<input type=submit name=mysub>
</form>
你最好对$stt进行tirm处理 去掉首尾的空格
$b_posts应该是个表名 以变量的形式引入不太妥当
下面的例子是我学PHP第5天写的 你参考下 不过千万不要学这种写法 仅适合初学者
<?php
require_once("./include/config.php");
require_once("./include/usercheck.php");
if (!empty($_GET["cs_Id"]))
{
$cs_Id = $_GET["cs_Id"];
}
elseif (!empty($_POST["cs_Id"]))
{
$cs_Id = $_POST["cs_Id"];
}
else
{
$cs_Id = 1;
}
$keyword = "";
if (!empty($_POST["keyword"]))
{
$keyword = $_POST["keyword"];
}
elseif (!empty($_GET["keyword"])){
$keyword = $_GET["keyword"];
}
else
{
$keyword = "";
}
if(isset($cs_Id))
{
// $sql="select * from tbarticle where cs_Id=".$cs_Id."";
// //echo $sql;
// $res=mysql_query($sql);
$wheresql = "";
if (!empty($keyword))
{
$wheresql = " and ae_Title like '%" .$keyword. "%'";
}
$sql1="select count(*) as amount from tbarticle where cs_Id=".$cs_Id.$wheresql." ";
//echo $sql1;
$result=mysql_query($sql1);
//获取总数据量
$row=mysql_fetch_array($result);
$amount=$row['amount'];
//exit;
//获取当前页数
if(isset($_GET['page']))
{
$page=intval($_GET['page']);
}
else
{
$page=1;
}
//每页数量
$page_size=2;
//计算总共有多少页
if($amount==0)
{
echo"没有任何数据";
exit;
}
if($amount < $page_size)
{
$page_count=1;
}
//if($amount%$page_size){//拿总数据除以每页的总数,如果有余数,那么总页数等于商+1
$page_count=ceil($amount/$page_size);
//翻页链接
$page_string='';
if ($page==1)
$page_string='第一页|<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.($page+1).'&keyword='.$keyword.'>下一页</a>|';
elseif(($page==$page_count||($page_count==0)))
$page_string='<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.($page-1).'&keyword='.$keyword.'>上一页</a>|<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.$page_count.'&keyword='.$keyword.'>尾页</a>';
elseif(($page>1)&&($page<$page_count))
$page_string='<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.($page-1).'&keyword='.$keyword.'>上一页</a>|<a href=classcontentshow.php?cs_Id='.$cs_Id.'&page='.($page+1).'&keyword='.$keyword.'>下一页</a>|';
//||($page_count==0)
//获取数据
$a=($page-1)*$page_size;
$b=$page_size;
$query_s="select * from tbarticle where cs_Id=".$cs_Id.$wheresql." order by ae_Id limit $a,$b";
$result_s=mysql_query($query_s);
//echo $query_s;
//$rows=mysql_fetch_assoc($res);
echo"<table border=1>";
while($rows=mysql_fetch_array($result_s))
{
$rows["ae_Date"]= date("Y-m-j H:i:s",$rows["ae_Date"]);
echo "<tr>";
echo "<td align='left' valign='middle'><a href='articlereadshow.php?ae_Id=".$rows["ae_Id"]."' target=_blank><font>".$rows["ae_Title"]."[".$rows["ae_Date"]."]</font></a>  ";
echo "</tr>";
}
echo "</table>";
echo '一共有'.$page_count.'页<p>';
echo '当前为第'.$page.'页<p>';
echo $page_string;
}
//echo "test";
//echo $keyword;
//print_r($_POST);
?>
<form name=xxx action="classcontentshow.php" method="post">
<input type=text name=keyword value="<? echo $keyword;?>">
<input type=hidden name=cs_Id value="<? echo $cs_Id;?>">
<input type=submit name=mysub>
</form>
2008-10-04
展开全部
id=$_GET['id'],看不出来有什么作用。
你把出错的地方列出来。
你把出错的地方列出来。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询