dream12001 2013-07-30 10:52
浏览 31
已采纳

当我在搜索脚本中输入空格超过4次时,为什么会显示我的完整表格

hello every one i have a simple phpmysql search engine script its working very fine but i found a big problem in it the problem is whenever blank space is entered more than four times in a search box it displays complete data of my table please tell me how to resolve this problem and yes if two or space is added after some string then also this problem occur.

$button = $_GET ['submit'];
$search = $_GET ['search']; 
if(strlen($search)<=1)
echo "Invalid search";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","9889922527");
mysql_select_db("specifications");

$search_exploded = explode (" ", $search);

foreach($search_exploded as $search_each)
{
@$x++;
if($x==1)

@$construct .="keyword LIKE '%$search_each%'";
else
$construct .="AND keyword LIKE '%$search_each%'";
}

$constructs ="SELECT * FROM search WHERE $construct";
$run = mysql_query($constructs);

@$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>";
else
{ 

echo "$foundnum results found !<p>";

$per_page = 20;
@$start = $_GET['start'];
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0; 
$getquery = mysql_query("SELECT * FROM search WHERE $construct  LIMIT $start,     $per_page");

while($runrows = mysql_fetch_assoc($getquery))
{
$image = $runrows['image'];
$name = $runrows ['name'];
$price = $runrows ['price'];
$url = $runrows ['url'];
echo "<link rel='stylesheet' href='search.css' type='text/css' />";
echo "<div class='cat-logo'><img width='200' height='180' src='$image' /><br/><a       href='$url'><font class='cat-head'><b>$name</b></font></a><br/><font class='cat-pr'>$price</font></div>" ;
//echo "<a href='$url'><b>$name</b></a><br>$price<br><a href='$url'>$url</a><p>";

}
  • 写回答

1条回答 默认 最新

  • dongshu9458 2013-07-30 11:17
    关注

    Tried to understand and cleanup your code (although some if/else loops seems to be unclosed in your example). Added some minimal escaping and added the trim (as suggested in the comments) to prevent the selection of all data. I also added a limit 1000 (to limit the amount of results a query could max generate:

    $button = mysql_real_escape_string($_GET['submit']);
    $search = trim(mysql_real_escape_string($_GET['search'])); 
    
    if(strlen($search)<=1) {
      echo "Invalid search";
    } else {
      echo "You searched for <b>$search</b> <hr size='1'></br>";
      mysql_connect("localhost","root","9889922527");
      mysql_select_db("specifications");
    
      $search_exploded = explode (" ", $search);
    
      $x = 0;
      $construct = null;
      foreach($search_exploded as $search_each)
      {
        $x++;
        if($x==1) {
          $construct .="keyword LIKE '%$search_each%'";
        } else {
          $construct .="AND keyword LIKE '%$search_each%'";
        }
    
        $constructs ="SELECT * FROM search WHERE $construct";
        $run = mysql_query($constructs);
    
        @$foundnum = mysql_num_rows($run);
    
        if ($foundnum==0) {
          echo "Sorry, there are no matching result for <b>$search</b>.</br></br>";
        } else { 
          echo "$foundnum results found !<p>";
    
          $per_page = 20;
          @$start = mysql_real_escape_string($_GET['start']);
          $max_pages = ceil($foundnum / $per_page);
          if(!$start) {
            $start=0; 
            $getquery = mysql_query("SELECT * FROM search WHERE $construct LIMIT $start, $per_page");
    
            while($runrows = mysql_fetch_assoc($getquery))
            {
              $image = $runrows['image'];
              $name = $runrows ['name'];
              $price = $runrows ['price'];
              $url = $runrows ['url'];
              echo "<link rel='stylesheet' href='search.css' type='text/css' />";
              echo "<div class='cat-logo'><img width='200' height='180' src='$image' /><br/><a       href='$url'><font class='cat-head'><b>$name</b></font></a><br/><font class='cat-pr'>$price</font></div>" ;
              //echo "<a href='$url'><b>$name</b></a><br>$price<br><a href='$url'>$url</a><p>";
            }
          }
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)