2016-03-22 11 views
0

私はCompでグループと働いています。 PHP、mySQL、HTMLを使ったこのWebサイトプロジェクトのSciクラスです。検索機能に問題があります。ここで MySQL/PHP検索機能?

は完全なコードです:

<html> 

<head> 
    <title>US Cities Database</title> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
    <link rel="stylesheet" href="index.css" type="text/css" /> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 
</head> 

<script> 

    function doWhenReady() { 
    $('a').click(showMap); 
    }; 

    function showMap(event) { 
    var element = $(event.currentTarget); 
    var lat = element.attr('lat'); 
    var long = element.attr('long'); 
    var url = "https://maps.google.com/maps?q="+lat+","+long+"&h1=es&z=12&t=m&output=embed"; 
    $('iframe').attr('src', url); 
    } 

    $(document).ready(doWhenReady); 

</script> 

<body class="container"> 

    <h4>US Cities Zip Code and Latitude/Longitude Locator</h4> 
    <p><strong>Make sure to enter the smaller Zip Code in the first box, and the larger Zip Code in the second box.</strong></p> 
    <form action="index.php" method="POST" class="form-inline"> 
    <div class="form-group"> 
     <label for="city">City</label> 
     <input type="text" class="form-control" name="city" /> 
    </div> 
    <div class="form-group"> 
     <label for="state">State</label> 
     <input type="text" class="form-control" name="state" /> 
    </div> 
    <div class="form-group"> 
     <label for="zipFrom">Zipcode from:</label> 
     <input type="text" class="form-control" name="zipFrom" /> 
    </div> 
    <div class="form-group"> 
     <label for="zipTo">to:</label> 
     <input type="text" class="form-control" name="zipTo" /> 
    </div> 
    <button type="submit" class="btn btn-default btn-primary">Search</button> 

    </form> 
    <hr> 

    <div class="row"> 
    <div class="col-md-5 scrollable"> 
     <div class="search-results"> 
     <h4>Search results:</h4> 

<?php 

    $host = 'localhost'; 
    $username = 'caleyhalpern'; 
    $password = ''; 
    $dbName = 'project224'; 

    $db = mysqli_connect ($host, $username, $password, $dbName); 
    if (!$db) { 
    die("Failed to connect to MySQL: (" . mysqli_connect_errno() . ") " . $mysqli->connect_error); 
    } 

    $searchCity = $_POST['city']; 
    $searchState = $_POST['state']; 
    $searchZipCode = $_POST['zipcode']; 
if ((isset($searchCity) || isset($searchState)) && (($searchCity != '') || ($searchState != ''))) 
    $query = 'SELECT * FROM zips WHERE city LIKE "%'.$searchCity.'%" AND state LIKE "%'.$searchState.'%"'; 
    $results = mysqli_query($db, $query); 
    $resultArray = mysqli_fetch_all($results, MYSQLI_ASSOC); 
?> 

     <table class="table table-striped table-condensed small"> 
      <thead> 
      <tr> 
       <th>Show</th> 
       <th>Zip</th> 
       <th>State</th> 
       <th>City</th> 
       <th>Lat</th> 
       <th>Long</th> 
      </tr> 
      </thead> 

      <tbody> 
<?php 
    foreach($resultArray as $city) { 
     echo "<tr>";      
     echo '<td><a href="#" lat="'.$city['lat'].'" long="'.$city['lng'].'">Show</a></td>'; 
     echo "<td>".$city['zip']."</td>"; 
     echo "<td>".$city['state']."</td>"; 
     echo "<td>".$city['city']."</td>"; 
     echo "<td>".$city['lat']."</td>"; 
     echo "<td>".$city['lng']."</td>"; 
     echo "</tr>";      
    } 
?> 
      </tbody> 
     </table> 
<?php 
    } 
?>   
     </div> 
    </div> 
    <div class="col-md-7"> 
     <div class="map"> 
     <h4>Map:</h4> 

     <iframe src=""></iframe> 
     </div> 
    </div> 
    </div> 

</body> 

</html> 

申し訳ありませんが、それは一種の長いのですが、あなたは、PHPのセクションまでスクロールした場合、あなたは「...(IF((ISSET」を参照してくださいよ、とそれは私たちが問題を抱えているところです。都市や州の検索機能がありますが、郵便番号検索機能に問題があります。ユーザーが2つの郵便番号を入力するようにしようとしています。最初の検索ボックスに入り、大きい郵便番号が2番目の検索ボックスに表示されます(例:82190、2番目:92120)。ユーザーが入力した2つの郵便番号の間にあるすべての郵便番号を検索します私は検索機能が都市/州のものと似ていることを理解しています。 PHPとMySQLの新機能で、少し苦労しています。

+0

データベースであなたの 'zipcode'列のデータ型は何ですか? –

+1

**警告**:mysqliを使用する場合は、パラメータ化されたクエリと['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)を使用してユーザを追加する必要がありますデータを照会に追加します。 **重大な[SQLインジェクションのバグ](http://bobby-tables.com/)を作成するため、これを達成するために文字列補間または連結を使用しないでください。 **決して '$ _POST'データを直接クエリに入れないでください。これらのことに注意しようとすると、忘れてしまうと深刻な問題になることがあります。パラメータ化されたクエリを使用すると、簡単な間違いでコードが破損する可能性が低くなります。 – tadman

+0

あなたはあなたの質問に 'AND'ではなく' ... OR state LIKE ... 'をしたくないと確信していますか? – Qirel

答えて

-1

まず、次の2つの郵便番号フィールドが数値であることを確認する必要がある、とされていない場合は、デフォルト値を設定し、$zipFromため00000$zipToため99999を言います。

$zipFrom = $_POST['zipFrom']; 
if (!is_numeric($zipFrom)) 
    $zipFrom = 0; 

同様に$zipToの値を処理します。 $zipFrom <= $zipToもチェックし、そうでなければスワップする必要があります。 (常に、常に、常にプログラムは防衛的です!)その後、クエリ自体は簡単です。ここでは、準備されたステートメントでそれを行う方法は次のとおりです。

$query = 'SELECT * FROM zips 
      WHERE zipcode >= ? 
       AND zipCode <= ?'; 
$stmt = $db->prepare($query); 
$stmt->bind_param('ii', $zipFrom, $zipTo); 
$stmt->execute(); 
$rslt = $stmt->get_result(); 
$resultArray = $rslt->fetch_all(MYSQLI_ASSOC); 

bind_paramへの呼び出しは、クエリで2疑問符プレースホルダに2つの変数をマッピングします。最初の引数('ii')は、値のデータ型を指定します。

私はエラー処理を読者の練習として残しています。

-2

あなたはこのような何かを試みることができる...

$query = "SELECT * FROM zips WHERE city LIKE '%".$searchCity."%')"; 
$result = mysqli_query($db, $query); 
if (mysqli_num_rows($result) > 0) { 
    while ($row = mysqli_fetch_assoc($result)) { 
     echo $row['zip']; 
     // echo any others you need 
    } 
}