Results 1 to 6 of 6
  1. #1

    * MySQL query build up

    Here is something challenging for me.... I have been thinking about it but never had to do it and it come.
    I will have a few drop downs selections, the user can select the ones he\she wants (these are basically conditions the user selects to do a search)
    Once those selections are made, how can I feed them into the query, some can be left blank and some are selected. I hope I am clear
    Web Hosting Review - Real Reviews by Real People Submit your company and get a link to your site, Read Reviews GET YOUR Voice HEARD and REVIEW your HOST NOW!!! Check out our IT Blog Tips & Tricks

  2. #2
    Join Date
    Feb 2003
    Location
    L.A. C.A.
    Posts
    335
    Assuming you know how to use MySQL queries,
    something along these lines would help you:
    Code:
    if (!empty($_POST['option1'])) $query.=" AND `option1` = '{$_POST['option1']}'";
    If you require any more assistance, please elaborate a little more.

  3. #3
    Ok this is very good. now what if the first one is blank. then when they get to optio1 (assuming it is the second option) it would read select AND 'option1 from table
    do you know what I mean
    Web Hosting Review - Real Reviews by Real People Submit your company and get a link to your site, Read Reviews GET YOUR Voice HEARD and REVIEW your HOST NOW!!! Check out our IT Blog Tips & Tricks

  4. #4
    Join Date
    Feb 2003
    Location
    L.A. C.A.
    Posts
    335
    Basically, you have 3 fields - option1, option2 and option3.

    With my method:
    Code:
    $query="SELECT * FROM `table` WHERE `type` = 'news'";
    if (!empty($_POST['option1'])) $query.=" AND `option1` = '{$_POST['option1']}'";
    if (!empty($_POST['option2'])) $query.=" AND `option1` = '{$_POST['option2']}'";
    if (!empty($_POST['option3'])) $query.=" AND `option1` = '{$_POST['option3']}'";
    You have to have that initial WHERE event, to prevent what you were saying.

  5. #5
    Ok makes sense..... Last question, how would you do the quotes.... see now
    `type`= 'news'" here you closed the quotes... would that not matter since you are appending to the query? Sorry I am not thinking.
    Web Hosting Review - Real Reviews by Real People Submit your company and get a link to your site, Read Reviews GET YOUR Voice HEARD and REVIEW your HOST NOW!!! Check out our IT Blog Tips & Tricks

  6. #6
    Join Date
    Feb 2003
    Location
    L.A. C.A.
    Posts
    335
    No, the quotes tell PHP that its a string, imagine:

    "this string is not valid PHP syntax;

    Because your not ending the quotes.

    http://us2.php.net/manual/en/languag...ors.string.php

    PHP Online manual is one of the best by far.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •