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

 <!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

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%'

The error you are getting is due to the fact that the OR is not being included in your SQL query. Hence the SQL in the error is keywords LIKE '%%' keywords LIKE '%%' keywords LIKE '%%' keywords LIKE '%%' (no ORs). The reason for this is that you are setting $i=0 just before $i++ in your loop. Thus $i is always 1 and the string with the OR included is never appended. Set $i=0 outside the loop.

The error message came because $i=0; is inside the loop. Move it outside.

Also avoid using blank strings.

Here's what I find a better way to build a WHERE clause:

$ors = array();
foreach ...
{
    if (...)
        $ors[] = 'keywords LIKE ...'
}
$or_str = implode(' OR ', $ors);

I find that simpler than special-casing the first or last item in an OR or AND list.

Meanwhile, you should consider using FULLTEXT instead of a bunch of LIKEs.