2012-04-06 82 views
-1

我有我的我的网站上搜索一个问题,我不明白为什么它给我这个错误搜索用PHP在我的网站

这是我的表单代码

<form action="index.php?cat=search_results&learn_id=1" method="post"> 
     <div id="topSearchBodyStyle"> 
     <input type="text" name="search" class="topSearchTextBackground" /> 
     </div> 
     <div id="topSearchButtonStyle"> 
     <input type="submit" name="submit" class="topSearchButtonBackground" value="" /> 
     </div> 
    </form> 

,这是其中有PHP代码页

<?php 

    $getSearch = $_POST['search']; 
    trim($getSearch); 

    if(!get_magic_quotes_gpc()) { 
     $getSearch = addslashes($getSearch); 
     } 

    $connectToDb = "select * from tutorials where tutorial_title like '%.$getSearch.%'"; 
    $searchResults = $db->query($connectToDb) or die($db->error); 
    if ($searchResults){ 
     $numResultas = $searchResults ->num_rows; 
     echo "<p>Found : " . $numResultas . "</p>"; 
     while($row = mysqli_fetch_array($searchResults)) { 

      echo $row['tutorial_title'];  
     } 
    }else{ 
     echo "cant connect"; 
     } 
?> 

任何想法,为什么给我这个备注“未定义指数:搜索” ,为什么结果出来“0”

回答

1

从您给出的代码示例中,我看不到您得到的原因Undefined index: search,但是,您确定$_POST['search']实际上已设置?如果这是一些框架或其他项目的一部分,我已经看到了一些未设置$ _POST和$ _GET,你必须以另一种方式访问​​它们。

未定义的索引错误是否可能来自代码中的不同行?

如果您通过$_POST['search']获得值,则得到0结果的原因是因为您的SQL语句中有错误。

应该是:"select * from tutorials where tutorial_title like '%".$getSearch."%'"

最后,你应该使用mysql_real_escape_string,而不是addslashes的,或者更好prepared statements

+0

是的谢谢AndrewR你说得对,这工作很好,谢谢你的努力,我的兄弟。 – 2012-04-06 15:31:56