使用PHP

时间:2015-05-12 13:00:18

标签: php html checkbox

我创建了一个包含手机详细信息的数据库但是当我使用复选框添加过滤器时,它会给我错误

  

警告:mysql_fetch_assoc()期望参数1是资源,   第16行/home/mobodr/public_html/search.php中给出的布尔值

     

警告:mysql_fetch_assoc()期望参数1是资源,   第23行/home/mobodr/public_html/search.php中给出的布尔值

我的HTML是

<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>

我希望当我点击特定的复选框时,它会向我显示该品牌的手机。但是,当我尝试这样做时,它给了我错误。为此,我的PHP脚本是

<?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>";

}?>

2 个答案:

答案 0 :(得分:2)

字符串必须在引号中,将它们添加到$com

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

答案 1 :(得分:1)

你犯了两个错误: -

1.String将用@panther所说的双引号: -

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

2. ORDER BY必须在WHERE条款之后。

所以最终的查询将是: -

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

注意: - 也请停止使用mysql_ *,因为它们已被正式弃用。使用mysqli_ *或PDO。