2012-02-16 13 views
5

में एकाधिक फ़ील्ड के साथ MySQL डेटाबेस खोजें मैंने एक फॉर्म बनाया है जहां उपयोगकर्ता डेटाबेस खोज सकता है, और परिणाम इस बात पर निर्भर करता है कि उपयोगकर्ता फ़ॉर्म को कैसे भरता है।
उदाहरण के लिए, मेरे पास नाम, पता, शहर, राज्य और ज़िप फ़ील्ड है, और उपयोगकर्ता नाम और शहर के फ़ील्ड भरता है, परिणाम इनपुट को दर्शाते हैं। जब फॉर्म सबमिट करता है तो सभी रिकॉर्ड प्रदर्शित होते हैं। इस के लिए मैं इस बारे में:एक फॉर्म

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 />"; 
      } 
     } 
    ?> 

लेकिन इस मामले में एक उपयोगकर्ता एक क्षेत्र को खाली छोड़ सकते हैं।

+0

कोडिंग यहाँ दुर्भावनापूर्ण उपयोगकर्ताओं के खिलाफ सुरक्षित नहीं है। कृपया http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php पढ़ें। –

उत्तर

12

इस प्रयास करें:

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

हां। यह सही तरीका है! –

+0

यहां कोडिंग दुर्भावनापूर्ण उपयोगकर्ताओं के खिलाफ सुरक्षित नहीं है। कृपया http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php पढ़ें। –

+1

मुझे लगता है कि यह उत्तर सर्वोत्तम और ऊपर के रूप में चिह्नित है, लेकिन $ 9 में $ _POST ['field'] $ _POST [$ फ़ील्ड] नहीं होना चाहिए? मैं संपादित नहीं करना चाहता क्योंकि मैं सुनिश्चित करने के लिए पर्याप्त कुशल php-er नहीं हूं, या पता है कि यह क्यों लिखा गया है। यह मेरे लिए काम करता है अब मैंने इसे बदल दिया है, लेकिन क्या मैंने एक सुरक्षा छेद खोला है (पोस्ट इनपुट को संचरित किया गया है)। – digitaltoast

संबंधित मुद्दे