douza9835 2016-12-30 11:09
浏览 45
已采纳

我的搜索引擎项目结果页面上有些错误

 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8">
<title>Search Engine Project</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form action="action.php" method="GET" class="search_form result-form"   autocomplete="off">
    <span class="result-header">Server Search</span>
    <input type="text" name="k" class="input result_input" value="<?php echo  $_GET['k'] ?>">
    <input type="submit" name="search" value="Search Web" class="search">
    </form>
    </div>


   <script src="js/bootstrap.min.js" ></script>
   <script src="js/jquery-3.1.1.min.js" ></script>
   <script src="js/javascript.js"></script>
   </body>
   </html>
   <?php 
   $q=$_GET['k'];
  if(isset($_GET['search'])){
  header("Location:https://www.google.co.in/?  gfe_rd=cr&ei=oyBmWN3FNvPx8Afe7a7IDA&gws_rd=ssl#q=$q");
  }
  if(isset($_GET['image'])){
  header("Location:https://www.google.co.in/search? site=&tbm=isch&source=hp&biw=1280&bih=670&q=$q");
  }
  if(isset($_GET['video'])){
   header("Location:https://www.google.com/search?  q=$q&biw=1280&bih=670&tbm=vid&source=lnms&sa=X&ved=0ahUKEwiJ_ruKxpvRAhVFgI8KHeVRBK4Q_AUICigD&dpr=1");
     }





  if(isset($_GET['local'])){
     mysql_connect("localhost", "root", "");
   mysql_select_db("search_query");
   $k = $_GET['k'];

if($k == "") {
echo "";
}
else {
$terms = explode(" ", $k);
$query= "SELECT * FROM search_table WHERE ";

foreach($terms as $each)
{
    $i=0;
    $i++;

    if($i==1)
    {
        $query .= "keywords LIKE '%$each%' ";
    }


    else
    {
        $query .= "OR keywords LIKE '%$each%' ";

    }

    }



  //query
   $query=mysql_query($query) or die(mysql_error());;
   $numrows= mysql_num_rows($query);
    if($numrows>0)
    {
        while($row = mysql_fetch_assoc($query))
        {
            $id= $row['id'];
            $title= $row['title'];
            $description= $row['description'];
            $keywords= $row['keywords'];
            $link= $row['link'];

            echo "<div style=''><h2><a href='$link'>$title</a></h2>
            $description</div> <hr><br/> <br/>";
        }

    }

    else
    {

        echo"No results found for \"<b>$k</b>\"";

    }


  //disconnect
   mysql_close();
   }
  }

  ?>

The above is the php code and the code searches the database in the mysql db and displays the approtiate result........

But when in the home page where i type the required keyword with lots of space before it and press search it gives a error type message :

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'keywords LIKE '%%' keywords LIKE '%%' keywords LIKE '%%' keywords LIKE '%%' keyw' at line 1 error"

I dont know what to do . I googled a lot in search for the fix.....

Images

PHP Error:

enter image description here

Blank Space input:

enter image description here

  • 写回答

3条回答 默认 最新

  • duankuangxie9070 2016-12-30 11:13
    关注

    Your query failed because of blank values in LIKE clause.

    E.g. if user entered book cover

    Your existing query will look something like this:

    SELECT *
    FROM search_table
    WHERE keywords LIKE '%%'
    OR keywords LIKE '%%'
    OR keywords LIKE '%%'
    OR keywords LIKE '%%'
    OR keywords LIKE '%book%'
    OR keywords LIKE '%%'
    OR keywords LIKE '%cover%'
    

    To fix this, trim value $_GET['k'] first.

    $k = trim($_GET['k']);
    

    Then, filter out blank values in case user adds multiple spaces in between words.

    $terms = explode(" ", $k);
    $terms = array_filter($terms);
    

    Now, your query will be

    SELECT *
    FROM search_table
    WHERE keywords LIKE '%book%'
    OR keywords LIKE '%cover%'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测