Mysql query problem

Discussion in 'MySQL' started by kuttappan, Jun 6, 2010.

  1. #1
    Hi guys,

    Could someone please help correct the syntax in my query, please. It worked fine until I added a "BETWEEN" clause to it. Here's the script and form that I use.

    //script
    $type=$_GET['type'];
    $material=$_GET['material'];
    $brand=$_GET['brand'];
    $seller=$_GET['seller'];
    if (!isset($_GET['keyword'])) { $keyword= "%"; } else { $keyword= "%".$_GET['keyword']."%"; }
    if (!isset($_GET['max_price'])) { $max_price= "%"; } else { $max_price=$_GET['max_price']; }
    if (!isset($_GET['min_price'])) { $min_price= "%"; } else { $min_price=$_GET['min_price']; }


    mysql_select_db($db_name, $connection);
    $query_search_results = "SELECT * FROM $tbl_name WHERE (description LIKE ('$keyword')) AND (type LIKE ('$type')) AND (material LIKE ('$material')) AND (brand LIKE ('$brand')) AND (seller LIKE ('$seller')) AND (regular_price BETWEEN ('$max_price') AND ('$min_price'))";

    //Form
    <form action="search.php" method="get" name="search">
    <label>Keyword<br />
    <input name="keyword" type="text" size="15" maxlength="100" />
    <br /><br />
    </label>
    <label>Type<br />
    <select name="type" size="1">
    <option value="%" selected="selected"></option>
    <?
    $sql = "SELECT DISTINCT type FROM product ORDER BY type ";
    $result = mysql_query($sql,$connection);
    while ($row = mysql_fetch_assoc($result))
    {
    $option = $row['type'];
    echo '<option value="'.$option.'">'.$option.'</option>';
    }
    ?>
    </select><br /><br />
    </label>
    <label>Material<br />
    <select name="material" size="1">
    <option value="%" selected="selected"></option>
    <?
    $sql = "SELECT DISTINCT material FROM product ORDER BY material ";
    $result = mysql_query($sql,$connection);
    while ($row = mysql_fetch_assoc($result))
    {
    $option = $row['material'];
    echo '<option value="'.$option.'">'.$option.'</option>';
    }
    ?>
    </select><br /><br />
    </label>
    <label>Brand<br />
    <select name="brand" size="1">
    <option value="%" selected="selected"></option>
    <?
    $sql = "SELECT DISTINCT brand FROM product ORDER BY brand ";
    $result = mysql_query($sql,$connection);
    while ($row = mysql_fetch_assoc($result))
    {
    $option = $row['brand'];
    echo '<option value="'.$option.'">'.$option.'</option>';
    }
    ?>
    </select><br /><br />
    </label>
    <label>Seller<br />
    <select name="seller" size="1">
    <option value="%" selected="selected"></option>
    <?
    $sql = "SELECT DISTINCT seller FROM product ORDER BY seller ";
    $result = mysql_query($sql,$connection);
    while ($row = mysql_fetch_assoc($result))
    {
    $option = $row['seller'];
    echo '<option value="'.$option.'">'.$option.'</option>';
    }
    ?>
    </select><br /><br />
    </label>
    <label>Maximum Price<br />
    <input name="max_price" type="text" size="15" maxlength="20" />
    <br /><br />
    </label>
    <label>Minimum price<br />
    <input name="min_price" type="text" size="15" maxlength="20" />
    <br /><br />
    </label>
    <input name="submit" type="submit" value="GO"/><br />

    </form>
     
    kuttappan, Jun 6, 2010 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Always post any errors you get. Failing that, echo your sql statement without executing it so we can see what exactly it looks like when it gets submitted to the database.
     
    plog, Jun 7, 2010 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    before you put your queries in php, try executing it first in mysql.. why dont you get that query first.
     
    bartolay13, Jun 7, 2010 IP