搜索功能:错误

时间:2010-10-11 09:45:47

标签: php mysql html

我不确定如何纠正这些错误,有人可以帮助我。

的search.php

Notice: Undefined index: submit in C:\wamp\www\i-document\search.php on line 12
Notice: Undefined index: search in C:\wamp\www\i-document\search.php on line 13

点击“搜索”后:

Notice: Undefined variable: x in C:\wamp\www\i-document\search.php on line 39
Notice: Undefined variable: construct in C:\wamp\www\i-document\search.php on line 41

Thankz。

代码:

<?php

//Get data

$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'>";

      //connect to database
      mysql_connect("localhost","root","");
      mysql_select_db("idoc");


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

            foreach($search_exploded as $search_each);
            {

                //Construct Query

                $x++;
                if ($x==1)
                   $construct .= "file_name LIKE '%$search_each%'";
                else
                   $construct .= " OR file_name LIKE '%$search_each%'";

            }


      $construct = "SELECT * FROM document WHERE $construct";
      //echo out construct

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

     $foundnum = mysql_num_rows($run);

     if ($foundnum==0)
        echo "No results found";
     else
     {
        echo "$foundnum results found!<p>";

        while ($runrows = mysql_fetch_assoc($run))
        {
         //Get data
         $ref = $runrows['file_ref'];
      $filename = $runrows['file_name'];    $owner = $runrows['owner'];
         $url = $runrows['url'];
         echo "
        <table>   <tr>
    <td> $ref </td>
    <td> $filename </td>
    <td> $owner </td>
    <td><a href='$url'>$url</a></td>
      </tr>    </table>
         ";
        }

     }
  } }
?>

<form id="form1" method="GET" action="search.php">
  <table width="446" height="135" border="1" align="center">
    <tr>
      <td height="31" colspan="2" align="center" valign="middle" bgcolor="#990000">
        <span class="style1 style2">
          Search :
        </span>
      </td>
    </tr>
    <tr>
      <td width="374" height="96" align="center" valign="middle" bgcolor="#990000">
        <span class="style1 style2">
          <label>
            <div align="left">
              Keyword :
              <input name="search" type="text" id="search" size="40" />
            </div>
          </label>
        </span>
        <td width="56" align="center" valign="middle" bgcolor="#990000">
          <div align="left">
            <input type = "submit" name="submit" value="search" />
          </div>
      </td>
    </tr>
  </table>
</form>

7 个答案:

答案 0 :(得分:1)

使用未知数组时,应该在读取之前测试数组键是否存在。在您的情况下,错误消息表明提交搜索都不存在。

在阅读数组密钥之前,使用issetarray_key_exists检查数组密钥是否存在:

// setting default values
$button = '';
$search = '';

// assigning GET parameter values if existing
if (isset($_GET['submit'])) {
    $button = $_GET['submit'];
}
if (isset($_GET['search'])) {
    $search = $_GET['search'];
}

您还可以使用conditional operator cond-expr ? true-expr : false-expr获得更简洁的符号:

$button = isset($_GET['submit']) ? $_GET['submit'] : '';
$search = isset($_GET['search']) ? $_GET['search'] : '';

答案 1 :(得分:0)

这意味着$ _GET在发送表单之前不包含索引'submit'。您可以使用isset()禁用通知错误渲染或添加条件。

尝试增加和连接未定义的变量后。

答案 2 :(得分:0)

foreach($search_exploded as $search_each);
{

    //Construct Query

    $x++; // this is bad practice because you didn't initialize the $x var before your foreach
    if ($x==1)
        $construct .= "file_name LIKE '%$search_each%'"; //this also "could" cause problems because you didn't define the $construct in the higher scope so that it "might" not be accessable from outside the if statement. BUT sometimes this works
    else
        $construct .= " OR file_name LIKE '%$search_each%'";

}

这是php给你的两个“通知”:

Notice: Undefined variable: x in C:\wamp\www\i-document\search.php on line 39

Notice: Undefined variable: construct in C:\wamp\www\i-document\search.php on line 41

答案 3 :(得分:0)

看起来你的error_reporting设置为最高级别,所以你也会得到很多通知消息。

'未定义索引'意味着您的$ _GET [any]变量在提交前为空。

'未定义变量'表示您使用的是以前未定义的变量($x++)。

在这个error_reporting级别进行开发总是一个很好的做法,因为您将获得有关应该在何处改进代码的大量信息。

对于这个例子:

$x = 0;
$construct = '';
$button = !empty($_GET['submit']) ? $_GET['submit'] : NULL;
$search = !empty($_GET['search']) ? $_GET['search'] : NULL;

答案 4 :(得分:0)

这些只是通知,而不是错误。 但是,如果要避免这些,可以使用isset()

检查变量是否通过

而不是

$button = $_GET['submit'];
if (!$button) 

使用

if (isset($_GET['submit'])) 

答案 5 :(得分:0)

这意味着您的网址中缺少您的搜索索引。确认您导航到包含“&amp; search =”的网址。如果您要通过表单提交搜索关键字,请验证其方法属性是否设置为“获取

答案 6 :(得分:0)

  1. 未定义索引submitsearch(它们不在URL中)。请尝试改为:

    $ button = isset($ _ GET ['submit'])? $ _GET ['submit']:false; $ search = isset($ _ GET ['search'])? $ _GET ['search']:false;

  2. 如果您不希望在每个未定义的变量上获得E_NOTICE
  3. 需要来定义变量。

    foreach($ search_exploded为$ search_each){                 //定义变量                 $ construct ='';                 $ x = 0;

                //Construct Query
    
    
    
            $x++;
            if ($x==1)
               $construct .= "file_name LIKE '%$search_each%'";
            else
               $construct .= " OR file_name LIKE '%$search_each%'";
    
    
        }