0

Hi I am trying to create a search function using OOP PHP however when I run my query and enter false data I am still getting results. Results that are not in the database.

I feel like I am lacking something in my code,

Perhaps my query is wrong I'm not sure as I am new to the whole programming aspect.

Any help would be welcomed!

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';    
        }
             } }
 ?>
4

1 に答える 1

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 インジェクションの危険にさらされます。DB アクセスに MySQLi クラスを使用しているため、ここには 2 つのアプローチがあります。変数を自分でエスケープするか、パラメーター化されたクエリを使用して 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();
    

    プリペアド ステートメントの詳細については、PHP のマニュアルを参照してください。

パラメーター化されたクエリは、より安全で最新のアプローチと見なされますが、どちらの方法でも問題なく機能します。ただし、どちらかを行う必要があります。それらがないと、誰かがコードに引用符を入力するとすぐにプログラムが壊れ、サイトのハッキングに簡単に使用される可能性があります.

%最後のポイント:文字列の両端でa を使用する SQL でのワイルドカード検索は、非常に遅くなります。DB が小さい場合は問題ありませんが、DB が大きくなるにつれて、クエリは徐々に遅くなります。テーブルに数百以上のレコードがあると予想される場合は、別の検索方法を真剣に検討する必要があります。(ニーズに応じて、ここには多くのオプションがあるため、ここでは説明しませんが、少し調べて、何が最適かを確認してください)。

それが役立つことを願っています。

于 2012-11-04T14:42:54.527 に答える