Answer the question
In order to leave comments, you need to log in
How stupid is the logic?
I can not think over the logic when sampling (search). The person enters the parameters, but I want them all to be optional. I did this:
$name_user =$_POST['name_user'];
$surname_user =$_POST['surname_user'];
$city_user =$_POST['city_user'];
$resultArray = "SELECT * FROM `user` WHERE `name` ='$name_user' AND`surname`='$surname_user' AND `city` = '$city_user' ";
if ($name_user==""){
$resultArray = str_replace("`name` ='$name_user' AND","",$resultArray);
}
if ($surname_user==""){
$resultArray = str_replace("`surname`='$surname_user' AND","",$resultArray);
}
if ($city_user==""){
$resultArray = str_replace("`city` = '$city_user'","",$resultArray);
}
Answer the question
In order to leave comments, you need to log in
The logic is fundamentally wrong.
This sample should be built based on what fields are entered, and not vice versa, remove the "extra"
As an example
$condition = array();
if ($username != "") {
$condition[] = " `username` = ' ". $username ."'";
}
if ($surname != "") {
$condition[] = " `surname` = ' ". $surname ."'";
}
$SQL = "SELECT * FROM `users`";
if (count($condition) > 0) {
$SQL .= " WHERE ".implode(" AND ", $condition);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question