Made a search:

  • Title:
  • City:
  • Address:

And then the criterion of the enterprise, what you need to find. It goes like a checkbox.

function ext($text) { return mysql_real_escape_string(htmlspecialchars($text)); } if($_POST['title']!='') $title=ext($_POST['title']); else $title=''; if($_POST['street']!='') $street=ext($_POST['street']); else $street=''; if($_POST['city']!='') $city=ext($_POST['city']); else $city=''; if(isset($_POST['wifi'])) $prefix1='1'; else $prefix1=''; if(isset($_POST['bil'])) $prefix2='1'; else $prefix2=''; if(isset($_POST['sush'])) $prefix3='1'; else $prefix3=''; if(isset($_POST['kal'])) $prefix4='1'; else $prefix4=''; if(isset($_POST['kar'])) $prefix5='1'; else $prefix5=''; if(isset($_POST['nal'])) $prefix6='1'; else $prefix6=''; $db = mysql_query("SELECT * FROM tb_users WHERE street_cafe LIKE '$street' or name_cafe LIKE '$title' or city_cafe LIKE '$city' or prefix1 LIKE '$prefix1' or prefix2 LIKE '$prefix2' or prefix3 LIKE '$prefix3' or prefix4 LIKE '$prefix4' or prefix5 LIKE '$prefix5' or prefix6 LIKE '$prefix6'"); 

How do I shorten it? And make the search normal. The result of the work works only on the street name of the city, and with the criteria of the problem. Help solve the issue. Searches for only one parameter.

    1 answer 1

    I hope this is a learning task, in the future, think over the architecture better, so that you do not have to fence gardens.

     //массивы соответвий для обычных полей и чекбоксов $fields=array('title'=>'name_cafe','street'=>'street_cafe','city'=>'city_cafe'); $checkboxes=array('wifi'=>'prefix1','bil'=>'prefix2','sush'=>'prefix3', 'kal'=>'prefix4','kar'=>'prefix5','nal'=>'prefix6'); //составляем запрос: $req=array(); $vals=array(); foreach($fields as $k=>$field){ if(array_key_exists($k,$_POST)){ $req[]='`'.$field.'` like ?'; $vals[]='%'.$_POST[$k].'%'; } } foreach($checkboxes as $k=>$checkbox){ if(array_key_exists($k,$_POST)){ $req[]='`'.$checkbox.'`=1'; } } if($req){ //подключаемся к базе $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $db->prepare("SELECT * FROM tb_users WHERE ".implode(' or ',$req)); $db->execute($vals); $result = $db->fetchAll(); print_r($result); } 
    • Why does not want to work, the query is displayed: SELECT * FROM tb_users WHERE name_cafe like? or street_cafe like? or city_cafe like? 1 Although I entered the parameter name_cafe - Creative Service Repair-Pk
    • all right, these are pdo and placeholders. - FLK
    • You did not understand. The request does not work. Or I did not understand. I just did not use classes in the database. How do I bring in Russian? - Creative Service Repair-Pk
    • I understood everything, it will not work through the mysql_ * functions, for this I responded and showed an example of connection via PDO. for everything to work, you must specify the correct $ host (host), $ dbname (database name), $ user (login), $ pass (password) - FLK
    • So go ahead throw a link to the class. And yet with msqli such crap will not work? - Creative Service Repair-Pk