Hello,

I was thinking a way to search more then 3 fields using LIKE my sql query i tried this way but its getting all the products not for the one it searched for

if(isset($_POST['search_now'])){ 
          if(isset($_GET['go'])){ 
              if(preg_match("/^[  a-zA-Z0-9]+/", $_POST['brand'] || $_POST['model'] || $_POST['new'])) { 
                  $new   = $_POST['new'];;
                  $brand = $_POST['brand']; 
                  $model = $_POST['model']; 
                  $mtrl  = $_POST['material'];
    $query1 = "SELECT * FROM products WHERE brand LIKE '%$brand%' OR model LIKE '%$model%' OR mtrl LIKE '%$papers%' ";
    $prod_set  = mysqli_query($connection, $query1);

Are each of your search values (brand, model and paper) actual values?
If they are empty values then they will return everything as
LIKE '%$brand%' is equivalent to LIKE '%%' if $brand is an empty value, which matches all values in that column.

@hericles is right, that is what happens. The variable $paper never gets a value. Your if on line 3 is probably not what you want. My guess is you want this:

if (
    preg_match("/^[ a-zA-Z0-9]+/", $_POST['brand']) && 
    preg_match("/^[ a-zA-Z0-9]+/", $_POST['model']) && 
    preg_match("/^[ a-zA-Z0-9]+/", $_POST['new'])
)

Meaning all three search terms need to have a value.

still facing the same when i search 1 query it gives me all prodcuts stored in the database not only the one which i search for

this is the modified code

 if(isset($_POST['search_now'])){ 
          if(isset($_GET['go'])){ 
              if(preg_match("/^[  a-zA-Z0-9]+/", $_POST['brand']) &&
                 preg_match("/^[  a-zA-Z0-9]+/", $_POST['model']) &&
                 preg_match("/^[  a-zA-Z0-9]+/", $_POST['material'])) { 
                  $new   = $_POST['new'];
                  $used  = $_POST['used'];
                  $year  = $_POST['year']; 
                  $box   = $_POST['box'];
                  $papers= $_POST['papers'];
                  $brand = $_POST['brand']; 
                  $model = $_POST['model']; 
                  $mtrl  = $_POST['material'];
    $query1 = "SELECT * FROM products WHERE brand LIKE '%$brand%' OR model LIKE '%$model%' OR mtrl LIKE '%$papers%' ";
    $prod_set  = mysqli_query($connection, $query1);

Still the same issue. If the three fields are filled (which you check), but papers isnt, you'll get mtrl LIKE %% which selects all records.

if(preg_match("/^[  a-zA-Z0-9]+/", $_POST['brand']) &&
                 preg_match("/^[  a-zA-Z0-9]+/", $_POST['model']) &&
                 preg_match("/^[  a-zA-Z0-9]+/", $_POST['material'])) { 
                  $brand = $_POST['brand']; 
                  $model = $_POST['model']; 
                  $mtrl  = $_POST['material'];
    $query1 = "SELECT * FROM products WHERE brand LIKE '%$brand%' AND model LIKE '%$model%' AND mtrl LIKE '%$mtrl%' ";

here is my updated code. like if 3 fieds are filled then its finding correctly but if like someone only go for brand and rest he feels to be empty so can show or filter brand only does it make sense though??

so if i want to add more 2 to 3 fields so in that view

Make those parameters optional, so only add the WHERE clause for a column, if that valkue actually has a value. Like this:

$query1 = " SELECT * FROM products WHERE 1=1 ";

$brand = $_POST['brand'];
if (!empty($brand) && preg_match("/^[ a-zA-Z0-9]+/", $brand))
{
    $query1 .= " AND brand LIKE '%$brand%' ";
}

//repeat

Here is the form to make you thing confirm what i am looking for let me try your given query as well but didn't understood WHERE 1=1

        <form method="POST" action="watch_search.php">
        <div class="newrow">
            <div id="search_box">
                <h4>CONDITION</h4>

                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="new" placeholder="NEW" />
                    <input type="text" class="field1" name="used" placeholder="USED" />
                    <input type="text" class="field1" name="year" placeholder="YEAR" />
                </div>
                </div>
            </div>

            <div id="search_box">
                <h4>BRAND</h4>

                <div class="table">
                <div class="search">
                    <select name="brand">
                    <?php 
                        $get_prod = find_all_brands();
                        echo "<option>Brand</option>";
                        while($prod_row = mysqli_fetch_assoc($get_prod)) {
                            echo "<option>" . $prod_row["brand"] . "</option>";
                        }
                    ?>  
                    </select>
                </div>
                </div>
            </div>

            <div id="search_box">
                <h4>MODEL</h4>

                <div class="table">
                <div class="search">
                    <select name="model">
                    <option>Model</option>
                    <?php 
                        $get_prod = find_all_models();  
                        while($prod_row = mysqli_fetch_assoc($get_prod)) {
                            echo "<option>" . $prod_row["model"] . "</option>";
                        }
                    ?>
                    </select>
                </div>
                </div>
            </div>

            <div id="search_box">
                <h4>CREDENTIALS</h4>

                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="box" placeholder="BOX" />
                    <input type="text" class="field1" name="papers" placeholder="PAPERS" />
                    <input type="text" class="field1" name="GUARANTEE" placeholder="GUARANTEE" />
                </div>
                </div>
            </div>

            <div id="search_box">
                <h4>MATERIAL</h4>

                <div class="table">
                <div class="search">
                    <select name="material">
                        <option>Material</option>
                    <?php 
                        $get_prod = find_all_material();    
                        while($prod_row = mysqli_fetch_assoc($get_prod)) {
                            echo "<option>" . $prod_row["mtrl"] . "</option>";
                        }
                    ?>  
                    </select>
                </div>
                </div>
            </div>
        <div class="clear"></div>
        </div>

        <div class="newrow">
            <div id="search_box">
                <h4>LOCATION</h4>

                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="location" placeholder="+ LOCATION" />
                </div>
                </div>
            </div>

            <div id="search_box">
                <h4>SELLER</h4>

                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="seller" placeholder="SEARCH ID, NAME, TEL" />
                </div>
                </div>
            </div>

            <div id="search_box">
                <h4>REFERENCE</h4>

                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="reff" placeholder="SEARCH" />
                </div>
                </div>
            </div>

            <div id="search_box">
                <h4>PRICE</h4>

                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="totalp" placeholder="TOTAL %" />
                    <input type="text" class="field1" name="totalpr" placeholder="TOTAL $" />
                </div>
                </div>
            </div>

            <div id="search_box">
                <h4>HISTORY</h4>

                <div class="table">
                <div class="search">
                    <select name="history">
                        <option>SAVED SEARCH</option>
                    </select>
                </div>
                </div>
            </div>
        <div class="clear"></div> 
            <input type="submit" name="search_now" value="SEARCH NOW" class="btn" style="margin:25px 8px;"/>
        </div>
        </form>

1st one is working perfectly but the second one when i repeated its not working

this is my modified code

 if(isset($_POST['search_now'])) { 
          if(isset($_GET['go'])) { 
              $query1 = " SELECT * FROM products WHERE 1=1";
        $brand = $_POST['brand'];
        $model = $_POST['model'];
        $mtrl = $_POST['material'];
        if (!empty($brand) && preg_match("/^[ a-zA-Z0-9]+/", $brand))
        {
            $query1 .= " AND brand LIKE '%$brand%'";
        }
        if (!empty($model) && preg_match("/^[ a-zA-Z0-9]+/", $model))
        {
            $query1 .= " AND model LIKE '%$model%'";
        }
        if (!empty($mtrl) && preg_match("/^[ a-zA-Z0-9]+/", $mtrl))
        {
            $query1 .= " AND mtrl LIKE '%$mtrl%'";
        }
    } else { 
            header("Location:search_watch.php"); 
        }

@pritaeas Did you found any soutions or any help for me though

Thank You

Isn't the problem that your select always have a value.

<select name="model">
    <option value="">Model</option>

let me try that if it works but i need to show a default value though let me give a shot

no not working though when i select 1st value from the dropdown and the 2nd option did not mathced first filter is not working though

I have noticed only 1 query is working when i set 2nd one then its not working though

if (!empty($brand) && preg_match("/^[ a-zA-Z0-9]+/", $brand))
        {
            $query1 .= " AND brand LIKE '%$brand%'";
        } elseif (empty($brand)) {
            header("Location:search_watch.php"); 
        }
        if (!empty($model) && preg_match("/^[ a-zA-Z0-9]+/", $model))
        {
            $query1 .= " AND model LIKE '%$model%'";
        }
        /*if (!empty($mtrl) && preg_match("/^[ a-zA-Z0-9]+/", $mtrl))
        {
            $query1 .= " AND mtrl LIKE '%$mtrl%'";
        }*/

Did you update all select boxes with the empty value?

yes I updated with emty values as well but nothing happend i removed the value like this

 <form method="POST" action="watch_search.php">
        <div class="newrow">
            <div id="search_box">
                <h4>CONDITION</h4>
                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="new" placeholder="NEW" />
                    <input type="text" class="field1" name="used" placeholder="USED" />
                    <input type="text" class="field1" name="year" placeholder="YEAR" />
                </div>
                </div>
            </div>
            <div id="search_box">
                <h4>BRAND</h4>
                <div class="table">
                <div class="search">
                    <select name="brand">
                    <?php 
                        $get_prod = find_all_brands();

                        while($prod_row = mysqli_fetch_assoc($get_prod)) {
                            echo "<option>" . $prod_row["brand"] . "</option>";
                        }
                    ?>  
                    </select>
                </div>
                </div>
            </div>
            <div id="search_box">
                <h4>MODEL</h4>
                <div class="table">
                <div class="search">
                    <select name="model">
                    <?php 
                        $get_prod = find_all_models();  
                        while($prod_row = mysqli_fetch_assoc($get_prod)) {
                            echo "<option>" . $prod_row["model"] . "</option>";
                        }
                    ?>
                    </select>
                </div>
                </div>
            </div>
            <div id="search_box">
                <h4>CREDENTIALS</h4>
                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="box" placeholder="BOX" />
                    <input type="text" class="field1" name="papers" placeholder="PAPERS" />
                    <input type="text" class="field1" name="GUARANTEE" placeholder="GUARANTEE" />
                </div>
                </div>
            </div>
            <div id="search_box">
                <h4>MATERIAL</h4>
                <div class="table">
                <div class="search">
                    <select name="material">
                    <?php 
                        $get_prod = find_all_material();    
                        while($prod_row = mysqli_fetch_assoc($get_prod)) {
                            echo "<option>" . $prod_row["mtrl"] . "</option>";
                        }
                    ?>  
                    </select>
                </div>
                </div>
            </div>
        <div class="clear"></div>
        </div>
        <div class="newrow">
            <div id="search_box">
                <h4>LOCATION</h4>
                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="location" placeholder="+ LOCATION" />
                </div>
                </div>
            </div>
            <div id="search_box">
                <h4>SELLER</h4>
                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="seller" placeholder="SEARCH ID, NAME, TEL" />
                </div>
                </div>
            </div>
            <div id="search_box">
                <h4>REFERENCE</h4>
                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="reff" placeholder="SEARCH" />
                </div>
                </div>
            </div>
            <div id="search_box">
                <h4>PRICE</h4>
                <div class="table">
                <div class="search">
                    <input type="text" class="field1" name="totalp" placeholder="TOTAL %" />
                    <input type="text" class="field1" name="totalpr" placeholder="TOTAL $" />
                </div>
                </div>
            </div>
            <div id="search_box">
                <h4>HISTORY</h4>
                <div class="table">
                <div class="search">
                    <select name="history">

                    </select>
                </div>
                </div>
            </div>
        <div class="clear"></div> 
            <input type="submit" name="search_now" value="SEARCH NOW" class="btn" style="margin:25px 8px;"/>
        </div>
        </form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.