php - ask about advance search when user select all something?
Get the solution ↓↓↓Solution:
In that case your query fetches all entries with body and color set to 'All'. You get no data because there are no entries for that values.
Something like this should do the trick:
$query = "SELECT * FROM Draw";
$where = "";
if(isset($_POST["body"]) && $_POST["body"] != "") {
if($_POST["body"] != "All") {
$where .= "`Body`='" . $_POST["body"] . "'";
}
}
if(isset($_POST["color"]) && $_POST["color"] != "") {
if($_POST["body"] != "All") {
if ($where.length() > 0) {
$where .= " AND ";
}
$where .= "`Color`='" . $_POST["color"] . "'";
}
}
if ($where.length() > 0) {
$query .= " WHERE " . $where;
}
Additionally you should read up on SQL injections.
Answer
Solution:
You code is pretty dangerous and vulnerable to injection.
If you are using mysql_query() then shift tomysqli
orPDO.
Below is the code snippet with the logic for your case.
NOTE:: I am assuming you are usingmysqli_*
$con = // Your mysqli connection ;
$sql = "SELECT * FROM Draw WHERE 1=1 ";
$where = "";
if(isset($_POST["body"]) && $_POST["body"] != ''){
if(trim($_POST["body"]) != "All"){
$where .= " AND `Body` = '".mysqli_real_escape_string($con,$_POST["body"])."'" ;
}
}
if(isset($_POST["color"]) && $_POST["color"] !=''){
if(trim($_POST["color"]) != "All"){
$where .= " AND `Color` = '".mysqli_real_escape_string($con,$_POST["color"])."'" ;
}
}
$sql .= $where ;
execute $sql ;
Answer
Solution:
The simplest solution is to make the query in parts.
$query="SELECT * FROM Draw ";
if($_POST['body']!='All'){$where[1]='Body = "'.$_POST['body'].'"';}
if($_POST['color']!='All'){$where[2]='Color = "'.$_POST['color'].'"';}
if(count($where>0)){
$where=' WHERE '.implode(' AND ',$where);
}
$query.=$where;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non well formed numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.