【问题标题】:Object Orientated Search function PHP面向对象的搜索功能 PHP
【发布时间】:2012-11-04 14:02:47
【问题描述】:

您好,我正在尝试使用 OOP PHP 创建搜索功能,但是当我运行查询并输入错误数据时,我仍然得到结果。不在数据库中的结果。

我觉得我的代码中缺少一些东西,

也许我的查询是错误的我不确定,因为我是整个编程方面的新手。

欢迎任何帮助!

index.php

  <?php
 include("classes/class.House.inc"); 
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>UndergradPad</title>
 <link rel="stylesheet" type="text/css" href="css/style.css" />

 </head>
 <body>
 <div id="bodyWrapper">
 <div id"header">
 <img id="banner" alt="UnderGradPad Logo" src="images/banner.png"/>
 </div> <!-- End header -->

 <div id="search">
<h1>Find student accomodation</h1><br/>
<p> Location  </p>
    <form method="post" action="search.php" name="search" id="searchform">
    <input type="text" name="term" id="searchinput"/>
    <input type="submit" name="submit" id="searchsubmit" value=""/>
    </form>
 <div class="help">e.g. 'PO5' or 'Portsmouth'</div>
 </div> <!--End search -->
 </body>
 </html>

classes/class.House.inc

  <?php 
     include("connect/class.Database.inc");

    class House extends Database {

     public function search (){

        $query = "SELECT * FROM houses WHERE postcode like '%$term%'";

                $result = $this->mysqli->query($query);

                $num_result = $result->num_rows;    
                if($num_result > 0){
                    while($rows =$result->fetch_assoc()){               
                        $this->data[]=$rows;
                        //print_r($rows);
                    }           
                    return $this->data;
            } 
    } else {
        echo 'No Records Found';    
        }
             } }
 ?>

【问题讨论】:

  • $term 确实存在于搜索方法的范围内...当您调用 search() 时,将其作为参数传递

标签: php search mysqli


【解决方案1】:

第一点,$term变量没有定义。

您的意思可能是$_POST['term']?这是 PHP 为发布的数据定义的全局变量。

但是,我建议将变量作为函数的参数,因为它可以让您灵活地使用它而无需依赖发布数据。

例如:

function Search($term) {
    // now you can use $term as a local variable within the function.
}

...在调用它的代码中,将$_POST['term'] 作为参数传递。使用这样的东西:

$houseobject->Search($_POST['term']);

其次,您需要对您的 SQL 数据进行转义,否则您将面临 SQL 注入的风险。由于您使用 MySQLi 类进行数据库访问,因此这里有两种方法:自己转义变量,或使用参数化查询并让 MySQLi 为您完成工作。

  • 自己逃跑:

    $query = "SELECT * FROM houses WHERE postcode like '%".$this->mysqli->real_escape_string($term)."%'";
    $result = $this->mysqli->query($query);
    
  • 参数化查询:

    $query = "SELECT * FROM houses WHERE postcode like ?";  // ? placeholder in query
    $stmt = $this->mysqli->prepare($query);
    $stmt->bind_param("s", "%$term%");       // insert your variable into the placeholder (still need to add % wildcards)
    $stmt->execute();
    

    有关准备好的语句的更多信息,请参阅the PHP manual

参数化查询被认为是更安全和更现代的方法,但无论哪种方式都可以正常工作。但是,您必须做其中之一;没有它们,一旦有人在代码中输入引号,您的程序就会中断,并且很容易被用来入侵网站。

最后一点:在 SQL 中使用% 在字符串两端进行通配符搜索非常慢。如果你的数据库很小,你会没事的,但随着它的增长,查询会越来越慢。如果您希望表中有超过几百条记录,您应该认真考虑替代搜索方法。 (这里有很多选择,具体取决于您的需要,所以我现在不会介绍它们,但做一些研究,看看什么最适合您)。

希望对您有所帮助。

【讨论】:

  • @Sprudley,感谢您的回答,它确实提供了丰富的信息。我已经尝试过您的解决方案,但是我现在收到此错误:Warning: Missing argument 1 for House::search(), called in /Applications/XAMPP/xamppfiles/htdocs/undergradpad/search.php on line 25 and defined in /Applications/XAMPP/xamppfiles/htdocs/undergradpad/classes/class.House.inc on line 23 我哪里出错了?
  • @Gurtarandeep - 由于您已在 Search 函数声明中指定了参数,因此您还需要在调用函数时指定它。我在答案中提到了这一点——将 post 变量传递给函数,如下所示:$obj-&gt;Search($_POST['term']) 其中$obj 是您已经创建的 House 对象。
  • 我看到除了这个错误消息Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/undergradpad/search.php on line 26之外,它现在可以正常工作了,这只发生在查询返回为假时。这是什么意思
  • @Gurtarandeep - foreach 如果你在不是数组的东西上循环它会抛出这个错误。在这种情况下,即使您没有任何结果,您也需要确保数组已初始化。但这现在已经脱离了最初的话题,很难在像这样的 cmets 中回答更多的大问题。如果您还有其他问题,您可能需要将它们作为单独的问题提出。
  • 好的,我会发布一个新问题,谢谢您的帮助!非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 2019-03-16
  • 2010-11-29
相关资源
最近更新 更多