【问题标题】:Undefined index [php] [duplicate]未定义的索引 [php] [重复]
【发布时间】:2014-01-27 16:01:15
【问题描述】:

我得到了这个用于搜索的代码,但我遇到了问题。它运行良好,但在我单击搜索选项卡后,它显示错误。但是,如果我尝试输入查询进行搜索,它可以正常工作。

这些是错误:

Notice: Undefined index: searching in F:\Programs\wamp\www\a\search.php on line 45
Notice: Undefined index: find in F:\Programs\wamp\www\a\search.php on line 46
Notice: Undefined index: field in F:\Programs\wamp\www\a\search.php on line 47

代码如下:

<h2>Search</h2> 
 <form name="search" method="post" action="search.php">
 Seach for: <input type="text" name="find" /> in 
 <Select NAME="field">
 <Option VALUE="firstname">First Name</option>
 <Option VALUE="lastname">Last Name</option>
 <Option VALUE="location">Location</option>
 </Select>
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" value="Search" />
 </form>


 <?php 


 $searching = $_POST['searching'];
 $find = $_POST['find'];
 $field = $_POST['field'];

 //This is only displayed if they have submitted the form 

 if ($searching =="yes") 
 { 
 echo "<h2>Results</h2><p>"; 

 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 

 // Otherwise we connect to our Database 
 mysql_connect("localhost", "root", "root") or die(mysql_error()); 
 mysql_select_db("chess") or die(mysql_error()); 

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim ($find); 

 //Now we search for our search term, in the field the user specified 
 $data = mysql_query("SELECT * FROM members WHERE upper($field) LIKE'%$find%'"); 

 echo "<table border=1>";
echo "<tr><td>Codename</td><td>Location</td><td>Rating</td></tr>";



 //And we display the results 
 while($result = mysql_fetch_array( $data )) 
 { 
 echo $result['firstname']; 
 echo " "; 
 echo $result['lastname']; 
 echo "<br>"; 
 echo $result['location']; 
 echo "<br>"; 
 echo "<br>"; 
 } 

 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 



 ?>

【问题讨论】:

    标签: php mysql undefined-index


    【解决方案1】:

    您需要检查您的$_POST 变量是否使用isset() 设置,因为当您没有发布任何内容时它们没有设置。这就是导致通知出现的原因。

    所以你需要做类似的事情:

    $searching = isset($_POST['searching']) ? $_POST['searching'] : '';
    $find = isset($_POST['find']) ? $_POST['find'] : '';
    $field = isset($_POST['field']) ? $_POST['field'] : 
    

    【讨论】:

    • 它有效。谢谢 :) 不能在 10 分钟内接受答复。我稍后会接受它。
    【解决方案2】:

    您可以使用isset(),但在这里,您使用POST 方法来搜索结果,您应该使用GET,因此请使用GET 方法而不是POST,但请确保您使用isset() 也适用于 $_GET

    if(isset($_GET['searching'])) {
       //Some code
    }
    

    【讨论】:

    • $_GET?表单有method="post"
    • @putvande 阅读答案,而不仅仅是代码
    • 好的。我想我的评论太早了。
    • @putvande 没问题 :)
    猜你喜欢
    • 2011-06-18
    • 2013-05-22
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2018-04-14
    相关资源
    最近更新 更多