2

さまざまな方法でインターネットを検索しましたが、私の質問に対する答えが見つかりません。私は独学でウェブサイト開発を学んでおり、オンラインの不動産ウェブサイトを作成しようとしています。

物件を探しているときに問題が発生します。たとえば、特定の地域または郵便番号内のすべてのプロパティを検索したいと考えています。住所行 1、住所行 2、郵便番号、地域 (London SW1V など) などの複数のフィールドを検索しようとした場合。

以下は私が現在使用しているクエリであり、必要な方法で動作するように変更したいと考えています。

SELECT property.propertyid, property.propertyname
FROM   property
WHERE  property.address_1 LIKE '%txtSearchField%'
    OR property.address_2 LIKE '%txtSearchField%'
    OR property.postcode  LIKE '%txtSearchField%'
    OR property.area      LIKE '%txtSearchField%'
4

2 に答える 2

4

これは効率的ではありませんが、最も簡単でトラフィックの少ないページに適しています。

SELECT property.propertyid, property.propertyname
FROM   property
WHERE  CONCAT(property.address_1, ' ',
              property.address_2, ' ',
              property.postcode, ' ',
              property.area) LIKE '%txtSearchField%'

これはうまくいくはずです。

于 2012-05-15T09:43:07.550 に答える
2

In this SQL query construct you execute it no matter what is the value of txtSearchField (even if it is empty). You also forgot to put the dollar sign $ on front of this variable txtSearchField, that's why you cannot get any results from your input form, because you always search for text txtSearchField, not the content of the variable $txtSearchField. (I guess you use an input form with HTML text input which is called txtSearchField). Remember to set your HTML form's method to "post", because if you omit it, the default is "get".

If I`m right, you should rework your code in this way:

    <?php
    //Sanitize user input
    $txtSearchField = filter_var($_POST['txtSearchField'], FILTER_SANITIZE_STRING);
    //SQL query
    $query = "SELECT property.propertyid, property.propertyname
    FROM   property
    WHERE  CONCAT(property.address_1, ' ',
                  property.address_2, ' ',
                  property.postcode, ' ',
                  property.area) LIKE '%$txtSearchField%'" //see the $ sign here
//Finally, execute query and get result
$result = mysql_query ($query) or die ('SQL error occured: '.mysql_error());
while ($array = mysql_fetch_assoc($result)) {
  echo $result['address_1'].'/'.$result['address_2'].'/'.$result['postcode'].'/'.$result['area'].'<br/>';
}
    ?>
于 2012-05-15T10:03:39.510 に答える