2012-02-16 13 views
5

Kullanıcının veritabanında arama yapabildiği bir form oluşturdum ve sonuç kullanıcının formu nasıl dolduracağına bağlı.
Örneğin, ad, adres, şehir, eyalet ve posta alanım olduğunu ve kullanıcının ad ve şehir alanlarını doldurduğunu, sonuçların girişi yansıttığını varsayalım. Form gönderildiğinde tüm kayıtlar görüntülenir. Bunun için ben bunu yazmak: MySQL Veritabanını Birden Fazla Alana Bir Formda Ara

if(isset($_POST['submit'])) { 
     $sql = mysql_query("SELECT * FROM table WHERE name LIKE '%" . $_POST['name'] . "%' 
        OR address LIKE '%" . $_POST['address'] . "%' 
        OR city LIKE '%" . $_POST['city'] . "%' 
        OR state LIKE '%" . $_POST['state'] . "%' 
        OR zip LIKE '%" . $_POST['zip'] . "%'"); 
    } 


     <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> 
      <tr> 
       <td>Name:</td> 
       <td><input type="text" name="name" /></td> 
      </tr> 
      <tr> 
       <td>Address:</td> 
       <td><input type="text" name="address" /></td> 
      </tr> 
      <tr> 
       <td>City:</td> 
       <td><input type="text" name="city" /></td> 
      </tr> 
      <tr> 
       <td>State:</td> 
       <td><input type="text" name="state" /></td> 
      </tr> 
      <tr> 
       <td>Zip:</td> 
       <td><input type="text" name="zip" /></td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
       <td><input type="submit" name="submit" value="Search" /></td> 
      </tr> 
     </form> 
    </table> 

    <?php 
     if(isset($_POST['submit'])) { 
      while($row = mysql_fetch_array($sql)) { 
       echo $row['name'] . "<br />"; 
      } 
     } 
    ?> 

Ancak bu durumda, bir kullanıcı alanı boş bırakabilir

.

+0

burada kodlama kötü niyetli kullanıcılara karşı güvenli değildir. Lütfen http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php adresini okuyun. –

cevap

12

bu deneyin:

if(isset($_POST['submit'])) { 
    // define the list of fields 
    $fields = array('name', 'address', 'city', 'state', 'zip'); 
    $conditions = array(); 

    // loop through the defined fields 
    foreach($fields as $field){ 
     // if the field is set and not empty 
     if(isset($_POST[$field]) && $_POST[$field] != '') { 
      // create a new condition while escaping the value inputed by the user (SQL Injection) 
      $conditions[] = "`$field` LIKE '%" . mysql_real_escape_string($_POST[$field]) . "%'"; 
     } 
    } 

    // builds the query 
    $query = "SELECT * FROM TABLE "; 
    // if there are conditions defined 
    if(count($conditions) > 0) { 
     // append the conditions 
     $query .= "WHERE " . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative 
    } 

    $result = mysql_query($query); 
+0

Evet. Doğru yol! –

+0

Burada kodlama kötü niyetli kullanıcılara karşı güvenli değildir. Lütfen http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php adresini okuyun. –

+1

Bu cevabın en iyi ve en üst düzeyde işaretlendiğini görüyorum, ancak 9. satırda $ _POST ['alan'] $ _POST [$ alan] olmamalı mı? Emin olmak için yeterince yetenekli bir php-er olmadığım gibi düzenleme yapmak istemiyorum, ya da neden olduğu gibi yazıldığını biliyorum. Benim için çalışıyor, şimdi değiştirdim, ama bir güvenlik deliği açtım (post girdisi dezenfekte edildi). – digitaltoast

İlgili konular