PHP-MySQL search criteria

I developed a database with several fields - about 65 fields. I want to set some search criteria for my users, i.e. by selecting various field names from the drop-down menu. I cannot execute it using simple IF-ELSEIF-ELSE statements. It gives results only for the first IF statement, but ignores the rest.

Below is the html page:

<form name="frm" action="search_plants.php" method="get"> <table> <tr> <td width="155" height="24">Select Scientific Name:</td> <td width="275"> <select name="spnm" size="1"> <option value="0"> All</option> <option value="Abrus precatorius"> Abrus precatorius</option> <option value="Abutilon indicum"> Abutilon indicum</option> <option value="Abutilon theophrasti"> Abutilon theophrasti</option> </select> </td> </tr> <tr> <td>Select Family Name:</td> <td> <select name="fmnm" size="1"> <option value="0"> All</option> <option value="Fabaceae">Fabaceae</option> <option value="Malvaceae">Malvaceae</option> <option value="Mimosaceae">Mimosaceae</option> </select> </td> </tr> <tr> <td>Select Geographic Location:</td> <td> <select name="znm" size="1"> <option value="0"> All</option> <option value="North Gujarat">North Gujarat</option> <option value="South Gujarat">South Gujarat</option> <option value="South East Gujarat">South East Gujarat</option> <option value="Central Gujarat">Central Gujarat</option> <option value="Kachchh">Kachchh</option> <option value="Saurashtra">Saurashtra</option> </select> </td> </tr> <tr> <tr> <td>Select Marker:</td> <td> <select name="pnm" size="1"> <option value="0"> All</option> <option value="rbcL">rbcL</option> <option value="psbA-trnH">psbA-trnH</option> <option value="matK">matK</option> <option value="rpoC1">rpoC1</option> <option value="ITS2">ITS2</option> <option value="ycf5">ycf5</option> </select> </td> </tr> <tr> <td> <input type="submit" value="Go" name="action"> <label> <input type="reset" name="Reset" id="button" value="Reset"> </label> </td> </tr> </table> </form> 

And the form action page looks like this, depicting only part of the entire code. I want to give the user the ability to select any / all fields and be able to display it by selective search from the database. Please help me.

 if (($species == 0) || ($family == 0) || ($zones ==0 ) || ($marker == 0)) { if(($species==0) && ($family==0) && ($zones==0)) { $sql="SELECT * FROM `ncbi_bold_ plants` WHERE Marker LIKE '$marker%'" ; } elseif($species==0 && $family==0 && $marker==0) { $sql="SELECT * FROM `ncbi_bold_ plants` WHERE Zones LIKE '%$zones%'"; } elseif($family==0 && $zones==0 && $marker==0) { $sql="select * from `ncbi_bold_ plants` where Speciesname like '$species%'"; } elseif($species==0 && $zones==0 && $marker==0) { $sql="select * from `ncbi_bold_ plants` where Family like '$family%'"; } /* elseif($species==0 && $marker==0) { $sql="select * from `ncbi_bold_ plants` where Family like '$family%' AND Zones like '%$zones%'"; $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}"); } elseif($family==0 && $zones==0 && $marker==0) { $sql="select * from `ncbi_bold_ plants` where Speciesname like '$species%'"; $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}"); } elseif($species==0 && $family==0) { $sql="select * from `ncbi_bold_ plants` where Zones like '%$zones%' AND marker like '$marker%'"; $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}"); } elseif($species==0 && $zones==0) { $sql="select * from `ncbi_bold_ plants` where Family like '$family%' AND marker like '$marker%'"; $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}"); } elseif($species==0 && $marker==0) { $sql="select * from `ncbi_bold_ plants` where Family like '$family%' AND Zones like '%$zones%'"; $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}"); } */ else { $sql="SELECT * FROM `ncbi_bold_ plants` " ; } } else { $sql="select * from `ncbi_bold_ plants` where Speciesname like '$species%' and Family like '$family%' and Zones like '%$zones%' and Marker like '$marker%'"; } $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}"); 

please help me for the same. I cannot find a flaw in the if statement.

+4
source share
4 answers

Try this simple function:

 function create_query($p_species, $p_family, $p_zones, $p_marker) { $options = array( 'Speciesname' => $p_species, 'Family' => $p_family, 'Zones' => $p_zones, 'Marker' => $p_marker ); $cond = ''; $noopt = true; foreach ($options as $column => $value) { if ($value !== 0) { $noopt = false; if ($cond != '') $cond .= ' AND '; $cond .= "$column LIKE '%$value%'"; } } return $noopt ? false : "SELECT * FROM ncbi_bold_ plants WHERE $cond;"; } 

This function returns false if all parameters are equal to zero, otherwise it returns a query string.

Example # 1:

 $species = 'spectest'; $family = 0; $zones = 'zonestest'; $marker = 0; echo create_query($species, $family, $zones, $marker); 

Output:

 SELECT * FROM ncbi_bold_ plants WHERE Speciesname LIKE '%spectest%' AND Zones LIKE '%zonestest%'; 

Example # 2:

 $species = 0; $family = 0; $zones = 0; $marker = 0; var_dump(create_query($species, $family, $zones, $marker)); 

Output:

 bool(false) 

Or, if you want to return a query string without conditions instead of false , simply replace the return string in the function with:

 return $noopt ? "SELECT * FROM ncbi_bold_ plants;" : "SELECT * FROM ncbi_bold_ plants WHERE $cond;"; 


Now, if you want to do this on your way with a lot of IF, ELSEIF, and ELSE sentences, there is a solution:

Solution without function

(it works the same as the function above):

  if ($species == 0 && $family == 0 && $zones == 0 && $marker == 0) { $sql = "SELECT * FROM ncbi_bold_plants"; } elseif ($species == 0 && $family == 0 && $zones == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Marker LIKE '$marker%'"; } elseif ($species == 0 && $family == 0 && $marker == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Zones LIKE '$zones%'"; } elseif ($species == 0 && $zones == 0 && $marker == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Family LIKE '$family%'"; } elseif ($family == 0 && $zones == 0 && $marker == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%'"; } elseif ($species == 0 && $family == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Zones LIKE '$zones%' AND Marker LIKE '$marker%'"; } elseif ($species == 0 && $zones == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Family LIKE '$family%' AND Marker LIKE '$marker%'"; } elseif ($species == 0 && $marker == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Family LIKE '$family%' AND Zones LIKE '$zones%'"; } elseif ($family == 0 && $zones == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Marker LIKE '$marker%'"; } elseif ($family == 0 && $marker == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Zones LIKE '$zones%'"; } elseif ($zones == 0 && $marker == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Family LIKE '$family%'"; } elseif ($species == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Family LIKE '$family%' AND Zones LIKE '$zones%' AND Marker LIKE '$marker%'"; } elseif ($family == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Zones LIKE '$zones%' AND Marker LIKE '$marker%'"; } elseif ($zones == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Family LIKE '$family%' AND Marker LIKE '$marker%'"; } elseif ($marker == 0) { $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Family LIKE '$family%' AND Zones LIKE '$zones%'"; } else { $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Family LIKE '$family%' AND Zones LIKE '$zones%' AND Marker LIKE '$marker%'"; } // here you have your $sql ... now do whatever you want with your query echo $sql; 

This code has been tested with all possible situations, so if it still doesn’t work, your problem is not with the PHP example you specified above. I do not recommend , because this is a completely wrong approach, even for beginners.

If you still want to do this with IF / ELSE / ELSEIF, consider the condition (s) checking the IF variable is NOT IMPOSSIBLE to zero. You will get less code and an easier implementation. Something like that:

Miscellaneous aproach

 $species = 0; $family = 0; $zones = 0; $marker = 0; // default query w/out conditions $sql = "SELECT * FROM ncbi_bold_plants"; // conditions array $conditions = array(); // adding condition to array for every parameter <> 0 if ($species != 0) $conditions[] = "Speciesname LIKE '$species%'"; if ($family != 0) $conditions[] = "Family LIKE '$family%'"; if ($zones != 0) $conditions[] = "Zones LIKE '$zones%'"; if ($marker != 0) $conditions[] = "Marker LIKE '$marker%'"; // all we need now is to concatenate array elements with " AND " glue $sql_cond = join(" AND ", $conditions); // last thing, adding condition(s) to the main query (if there any) if ($sql_cond != '') $sql .= " WHERE $sql_cond"; // let see what we have now echo $sql; 

The first 4 lines are for testing purposes only. Now we have the same solution as the previous one, but with less code (only 8 lines).

Hope this helps.

+3
source

Why not create a dynamic drop down menu from the fields? E.g. (I use this on my own page, you will need to edit the SQL syntax / query accordingly

 echo "\n\t<select name='cat'>"; echo "\n\t\t<option value=''>Search all...</option>"; $connect = mysql_connect( <connection settings> ); $ctgTableCmd = "SELECT name FROM offliner.ctgtable ORDER BY name ASC"; $getCtg = mysql_query( $ctgTableCmd ); while( $ctgTable = mysql_fetch_array( $getCtg ) ) { $ctgVal = $ctgTable[0]; echo "\n\t\t<option value=$ctgTable[0]>" . ucfirst( $ctgTable[0] ) . "</option>"; } 
+1
source

you can do like this code. First, configure for yourself:

 <?php $where = ''; if(isset($_POST)) { foreach($_POST as $k=>$v) { $q[] = $k.' LIKE \'%'.$v.'%\''; } $where = implode(' AND ',$q); } if(!empty($where)) $sql = 'SELECT * FROM `ncbi_bold_ plants` WHERE '.$where; else $sql = 'SELECT * FROM `ncbi_bold_ plants`"; ...... ...... 
0
source
 try using following approach if($species) { $colum='Species'; $val=$family; } else if($family) { $colum='Family'; $val=$family; } else if($zones) { $colum='Zones'; $val=$zones; } else if($marker) { $colum='Marker'; $val=$marker; } else { $column=''; $val=''; } $sql="SELECT * FROM `ncbi_bold_ plants` WHERE $column LIKE '%".$val."%'" ; 
0
source

Source: https://habr.com/ru/post/1394958/


All Articles