首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索功能:错误

搜索功能:错误
EN

Stack Overflow用户
提问于 2010-10-11 17:45:47
回答 7查看 1.1K关注 0票数 0

我不知道如何改正这些错误,有人能帮我吗?

search.php

代码语言:javascript
复制
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

单击“搜索”后:

代码语言:javascript
复制
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

谢谢。

代码:

代码语言:javascript
复制
<?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>
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-10-11 17:53:11

在处理未知数组时,应该在读取数组键之前测试它是否存在。在您的情况下,错误消息显示提交和搜索都不存在。

在读取数组键之前,使用issetarray_key_exists检查数组键是否存在:

代码语言:javascript
复制
// 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用于更简洁的表示法:

代码语言:javascript
复制
$button = isset($_GET['submit']) ? $_GET['submit'] : '';
$search = isset($_GET['search']) ? $_GET['search'] : '';
票数 1
EN

Stack Overflow用户

发布于 2010-10-11 17:52:07

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

在尝试递增和连接未定义的变量之后。

票数 0
EN

Stack Overflow用户

发布于 2010-10-11 17:52:28

代码语言:javascript
复制
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在这里给你的两个“通知”:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3905091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档