【问题标题】:search engine has 2 errors when i search anything当我搜索任何内容时,搜索引擎有 2 个错误
【发布时间】:2014-02-18 09:50:32
【问题描述】:

我正在为我的网站构建自己的搜索引擎,当我搜索下面列出的任何错误时,我不断收到两个错误,结果确实出现了,但仍然显示错误。我该怎么做才能让它们消失?消失是指修复!谢谢

错误 1

"( ! ) Notice: Undefined variable: x in C:\wamp\www\search.php on line 21" 

错误 2

"( ! ) Notice: Undefined variable: construct in C:\wamp\www\search.php on line 23"

这是我的代码我的 index.php

<html>
<head>
<title>Title of your search engine</title>
</head>
<body>
<form action='search.php' method='GET'>
<center>
<h1>My Search Engine</h1>
<input type='text' size='90' name='search'></br></br>
<input type='submit' name='submit' value='Search source code' ></br></br></br>
</center>
</form>
</body>
</html>

这是我的 search.php 代码

<?php

$button = $_GET ['submit'];
$search = $_GET ['search']; 

if(!$button){
echo "you didn't submit a keyword";
}
else
{
if(strlen($search)<=1){
echo "Search term too short";
}else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","");
mysql_select_db("search");

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

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";

}

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

$foundnum = mysql_num_rows($run);

if ($foundnum==0){
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
}else
{
echo "$foundnum results found !<p>";

while($runrows = mysql_fetch_assoc($run))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];

echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a><p>
";

}
}

}
}

?>

【问题讨论】:

  • 使用前初始化变量...示例 $x=0;
  • 警告:您的代码对 SQL 注入开放!
  • 先定义$x和$construct

标签: php search-engine


【解决方案1】:
$x = 0;
$construct = '';  

在for循环之前定义

【讨论】:

    【解决方案2】:

    在使用它们之前声明你的变量

    在for循环之前声明为

    $x=0; $construct="";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多