super_sonic_ace Posted May 21, 2010 Share Posted May 21, 2010 Hi guys, Im trying to make a sql query where it searches for the fields i select from a form. The problem is i have been using the "like" operator so i can leave the search field black in the form if needs be. But this has mad a problem as it returns other results that are LIKE and not what i want. the only why i can think of doing this is to rebuild lots of statements with if isset, but thats not very good programing, the only other thin i can think of is to use if isset and then have it dynamically build the sql statement, but i don't know how to do this. can anyone help me please. $sql04 = "SELECT * FROM payments WHERE companyID LIKE '%".$_GET['company']."%' AND monthNo LIKE '%".$_GET['month']."%' AND salesmanID LIKE '%".$_GET['salesman']."%' AND stageID LIKE '%".$_GET['stage']."%'"; Quote Link to comment https://forums.phpfreaks.com/topic/202509-new-to-this-fourm-php-sql-search-script/ Share on other sites More sharing options...
andrewgauger Posted May 21, 2010 Share Posted May 21, 2010 dynamically build the sql statement Oh its not that bad: $fields=array("companyID" => "company", "monthNo" => "month", "salesmanID" => "salesman", "stageID"=>"stage"); $sql04="SELECT * FROM payments "; foreach ($fields as $key => $arg) { if(isset($_GET[$arg]) $sql04 .= "WHERE $key= {$_GET[$arg]} AND"; } $sql04=rtrim($sql04, " AND"); Quote Link to comment https://forums.phpfreaks.com/topic/202509-new-to-this-fourm-php-sql-search-script/#findComment-1061699 Share on other sites More sharing options...
super_sonic_ace Posted May 24, 2010 Author Share Posted May 24, 2010 how do i implement that code? I have never used arras in php. Quote Link to comment https://forums.phpfreaks.com/topic/202509-new-to-this-fourm-php-sql-search-script/#findComment-1062496 Share on other sites More sharing options...
ignace Posted May 24, 2010 Share Posted May 24, 2010 foreach ($fields as $key => $arg) { if(isset($_GET[$arg]) $sql04 .= "WHERE $key= {$_GET[$arg]} AND"; } Andrew, don't you notice something about this? Quote Link to comment https://forums.phpfreaks.com/topic/202509-new-to-this-fourm-php-sql-search-script/#findComment-1062507 Share on other sites More sharing options...
super_sonic_ace Posted May 24, 2010 Author Share Posted May 24, 2010 whats wrong with it, as i dont understand the logic. foreach ($fields as $key => $arg) where is the $key getting its value from? Quote Link to comment https://forums.phpfreaks.com/topic/202509-new-to-this-fourm-php-sql-search-script/#findComment-1062524 Share on other sites More sharing options...
andrewgauger Posted May 24, 2010 Share Posted May 24, 2010 yep, it repeats the where clause when it shouldn't. I suck. $fields=array("companyID" => "company", "monthNo" => "month", "salesmanID" => "salesman", "stageID"=>"stage"); $sql04="SELECT * FROM payments WHERE 1"; foreach ($fields as $key => $arg) { if(isset($_GET[$arg]) $sql04 .= " AND $key= {$_GET[$arg]}"; } Is there a better way? Quote Link to comment https://forums.phpfreaks.com/topic/202509-new-to-this-fourm-php-sql-search-script/#findComment-1062737 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.