PHP Scripts Development » General Web Development Forum

SQL statement with an unkown number of POST vars

(4 posts)

Great offers not only for geeks!


  1. User has not uploaded an avatar

    gigido
    Member

    Hi, I have linked your class script to my html form inorder to show the results of the criteria a user inputs for a search.

    this is the code i had which works prior to adding the pagination script, but i am having issues trying to figure out how it needs to be formatted to fit into this class script.

    if (empty($_POST['genre'])) { $where_genre = "true"; } else { $where_genre = "genre = '".$_POST['genre']."'"; }
    if (empty($_POST['year'])) { $where_year = "true"; } else { $where_year = "year = '".$_POST['year']."'"; }
    if (empty($_POST['title'])) { $where_title = "true"; } else { $where_title = "title LIKE '".$_POST['title']."'"; }
    
    $sql = mysql_query("SELECT * FROM listings WHERE $where_genre AND $where_year AND $where_title");

    any insight would be appreciated.

    thanks!

    Posted 5 months ago #
  2. Sorry I don't get it all, "true" can't be a part of the sql statement, do you want this?

    if (!empty($_POST['title'])) $where_title = "title LIKE '".$_POST['title']."'";

    Posted 5 months ago #
  3. User has not uploaded an avatar

    gigido
    Member

    basicaly if any of the 2 drop down menus or text field search bar is empty, i dont want it to search for empty values, but infact return all values in that particular field... thats why it was written as "true", but someone else gave that to me and it works before adding the pagination script here... Is there an alternative to ignoring blank values in in the drop down menu when searching for multiple criteria?

    Posted 5 months ago #
  4. if you have dynamic input you can CONCAT the sql parts like:

    $sql = "SELECT * FROM table WHERE 1";
    if (!empty($_POST['title'])) $sql .= " AND title LIKE '".$_POST['title']."'";
    if (!empty($_POST['some_var'])) $sql .= " AND some_col LIKE '".$_POST['some_var']."'";

    The firs "1" is only used if there is no other condition.

    Posted 5 months ago #

RSS feed for this topic

Reply

You must log in to post.