使用 PHP 中的复选框过滤器从 SQL 获取数据
Fetching data from SQL using checkbox filter in PHP
我创建了一个包含手机详细信息的数据库,但是当我使用复选框添加过滤器时出现错误
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
我的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>";
}?>
字符串必须在引号中,将它们添加在 $com
周围。
$dat = mysql_query("SELECT * FROM Devices2 WHERE comp='". $com ."' ASC);
^ ^
你犯了两个错误:-
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。
我创建了一个包含手机详细信息的数据库,但是当我使用复选框添加过滤器时出现错误
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
我的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>";
}?>
字符串必须在引号中,将它们添加在 $com
周围。
$dat = mysql_query("SELECT * FROM Devices2 WHERE comp='". $com ."' ASC);
^ ^
你犯了两个错误:-
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。