PHP MySQL multiple search query using option / select HTML form tags

前端 未结 2 1736
暖寄归人
暖寄归人 2021-01-26 01:58

I\'m trying to set up a basic MySQL LIKE search using two search fields. I wan\'t to have it so it\'s multiple optional search fields e.g. if(isset($_POST[\'city\']) || i

相关标签:
2条回答
  • 2021-01-26 02:38

    HTML

    <select multiple name="location[]" size="2">
    

    PHP

    $w     = array();
    $where = '';
    foreach ($_POST['location'] as $loc){
      $loc = mysql_real_escape_string($loc);
      $w[] = "location = '$loc'";
    }
    if (!empty($_POST['type'])){
      $w[] = "type LIKE '%".mysql_real_escape_string($_POST['type'])."%'";
    }
    if ($w) $where = implode(' OR ',$w);
    
    0 讨论(0)
  • 2021-01-26 02:38

    I am not sure what you want but try this:

    PHP:

    // mysql escape
    $locations = array_map("mysql_real_escape_string", $_POST['location']);
    // add locations to query string
    $locations = implode("','",$location);
    
    $query = "SELECT 
                  name, type, description, location, zip_code, phone_number
                  FROM search
                  WHERE location IN ('$locations')
                  ";
    if (isset($_POST['type']) && !empty($_POST['type'])){
      $query .= " or type LIKE '%".mysql_real_escape_string($_POST['type'])."%'";
    }
    
    0 讨论(0)
提交回复
热议问题