abhishek bagul
abhishek bagul

Reputation: 263

Fetching data from SQL using checkbox filter in PHP

I have created a database having the details of mobiles but when i go for adding the filter using checkbox it give me error

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/mobodr/public_html/search.php on line 16

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/mobodr/public_html/search.php on line 23

For which my HTML is

<form action='search.php' method='get'>
<section id="portal">
<input class="tick" type="checkbox" name="comp" value="LG">LG</input><br/>
<input class="tick" type="checkbox" name="comp" value="Motorola">Motorola</input><br/>
<input class="tick" type="checkbox" name="comp" value="Nokia">Nokia</input><br/>
<input class="tick" type="checkbox" name="comp" value="Microsoft">Microsoft</input><br/>
<input class="tick" type="checkbox" name="comp" value="Xolo">Xolo</input><br/>
<input class="tick" type="checkbox" name="comp" value="Micromax">Micromax</input><br/>
<input class="tick" type="checkbox" name="comp" value="Karbonn">Karbonn</input><br/>
<input class="tick" type="checkbox" name="comp" value="iBall">iBall</input><br/>
<input class="tick" type="checkbox" name="comp" value="Spice">Spice</input><br/>
<input class="tick" type="checkbox" name="comp" value="Videocon">Videocon</input><br/>
<input class="tick" type="checkbox" name="comp" value="Fly">Fly</input><br/>
<input class="tick" type="checkbox" name="comp" value="Zen">Zen</input><br/>
<input class="tick" type="checkbox" name="comp" value="LAVA">LAVA</input><br/>
<input class="tick" type="checkbox" name="comp" value="Sony">Sony</input><br/>
<input class="tick" type="checkbox" name="comp" value="Acer">Acer</input><br/>
<input class="tick" type="checkbox" name="comp" value="Lenovo">Lenovo</input><br/>
<input class="tick" type="checkbox" name="comp" value="Apple">Apple</input><br/>
<input class="tick" type="checkbox" name="comp" value="Idea">Idea</input><br/>
<input class="tick" type="checkbox" name="comp" value="Pansonic">Panasonic</input><br/>
<input class="tick" type="checkbox" name="comp" value="Oppo">Oppo</input><br/>
<input class="tick" type="checkbox" name="comp" value="samsung">Samsung</input><br/>

    </section>
    <input value="Search" type="submit" style="border-radius: 8px; background: #fff;"></input>
    </form>

I want when i click on the particular check box then it show me the mobiles of only that brand. But when i try to do so it gives me error. For this my PHP script is

<?php
$com = $_REQUEST['comp'];
  mysql_connect("localhost","","");
    //seclect db
    mysql_select_db("mobodr_mobile");
    //connection string
    $dat = mysql_query("SELECT * FROM Devices2 ORDER BY comp WHERE comp=". $com ." ASC" );
    $img = 'img';
$name = 'name';
$dis = 'dis';
$comp = 'comp';
$ebay = 'ebay';
$amazon = 'amazon';
$res = mysql_fetch_assoc($dat);
while ($res = mysql_fetch_assoc($dat)){echo "<figure><div style='text-align:center;'><img class='prev' src='" . $res[$img] . "'/></div><figcaption><a href='http://mobodroid.net/show.php?q=".  $res[$name] . "'><h3>" . $res[$name] . "</h3></a><img width='80px' height='40px' src='". $comimg ."'/><a class='mor' href='http://mobodroid.net/show.php?q=".  $res[$name] . "'>View Details</a></figcaption></figure>";

}?>

Upvotes: 0

Views: 1286

Answers (2)

pavel
pavel

Reputation: 27092

Strings has to be in quotes, add them around $com.

$dat = mysql_query("SELECT * FROM Devices2 WHERE comp='". $com ."' ASC);
                                                      ^          ^

Upvotes: 2

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You did two mistakes:-

1.String will be in double quotes as said by @panther:-

$dat = mysql_query("SELECT * FROM Devices2 ORDER BY comp WHERE comp='". $com ."' ASC" );

2.ORDER BY must came after WHERE clause.

So the final query will be:-

$dat = mysql_query("SELECT * FROM Devices2 WHERE comp='". $com ."' ORDER BY comp ASC" );

Note:- Also please stop using mysql_* because they are officially deprecated. Use mysqli_* or PDO.

Upvotes: 1

Related Questions