使用 php / mysql 使用可点击图像(输入)进行过滤
filter with clickable images (input) with php / mysql
我有一些小图像(输入类型=图像),我想将它们用作过滤器,例如如果我点击第一张图片,在 mysql 查询中我只想 SELECT 那些具有相应值的图片。
它似乎有效,但我收到 PHP 通知。我错过了什么?
这就是我目前所知道的。我附上了 MySql 通知和 table 结构的屏幕截图。
.thumbnails{height:60px;display:block;}.galery{height:200px;}
<form action="" method="post">
<input type="image" name="panama" value="panama" alt="panama" src="http://static1.cuyana.com/media/catalog/product/cache/5/gallery/0dc2d03fe217f8c83829496872af24a0/5/_/5_hat_1_2.jpg" class="thumbnails">
<input type="image" name="tophat" value="tophat" alt="tophat" src="http://www.villagehatshop.com/photos/product/giant/3509260S41/-/size-7-1-4.jpg" class="thumbnails">
</form>
<?php
$con = mysqli_connect("localhost","Melvin","") or die ("could not connect to server: " . mysqli_connect_error($con));
mysqli_select_db($con, "galerie") or die ("Could not connect to database: " . mysqli_error($con));
//$result = mysqli_query($con, "SELECT * FROM hats");
# neu ab hier
if($_POST['panama']) {
$result = mysqli_query($con, "SELECT * FROM hats WHERE hat_cat='Panamas'");
} elseif($_POST['tophat']) {
$result = mysqli_query($con, "SELECT * FROM hats WHERE hat_cat='Tophats'");
} else {
$result = mysqli_query($con, "SELECT * FROM hats");
}
# ende neu
while($row = mysqli_fetch_array($result)){
echo "<img src=".$row['hat_name']." class='galery'>";
}
?>
而不是
如果($_POST['panama']){
使用 if(isset($_POST['panama'])){
应该可以解决
你需要使用函数isset()所以你的代码会变成下面这样;
if(isset($_POST['panama'])) {
$result = mysqli_query($con, "SELECT * FROM hats WHERE hat_cat='Panamas'");
} elseif(isset($_POST['tophat'])) {
$result = mysqli_query($con, "SELECT * FROM hats WHERE hat_cat='Tophats'");
} else {
$result = mysqli_query($con, "SELECT * FROM hats");
}
发生的事情是你的代码
$_POST['panama']
"Assumes"(如果你愿意的话),数组项 "panama" 总是会被传递给脚本,但在你的实例中情况并非如此,你要么传递;
<input type="image" name="panama" value="panama"/>
<input type="image" name="tophat" value="tophat"/>
因此使用 isset() 将检查该项目是否存在!
我有一些小图像(输入类型=图像),我想将它们用作过滤器,例如如果我点击第一张图片,在 mysql 查询中我只想 SELECT 那些具有相应值的图片。
它似乎有效,但我收到 PHP 通知。我错过了什么?
这就是我目前所知道的。我附上了 MySql 通知和 table 结构的屏幕截图。
.thumbnails{height:60px;display:block;}.galery{height:200px;}
<form action="" method="post">
<input type="image" name="panama" value="panama" alt="panama" src="http://static1.cuyana.com/media/catalog/product/cache/5/gallery/0dc2d03fe217f8c83829496872af24a0/5/_/5_hat_1_2.jpg" class="thumbnails">
<input type="image" name="tophat" value="tophat" alt="tophat" src="http://www.villagehatshop.com/photos/product/giant/3509260S41/-/size-7-1-4.jpg" class="thumbnails">
</form>
<?php
$con = mysqli_connect("localhost","Melvin","") or die ("could not connect to server: " . mysqli_connect_error($con));
mysqli_select_db($con, "galerie") or die ("Could not connect to database: " . mysqli_error($con));
//$result = mysqli_query($con, "SELECT * FROM hats");
# neu ab hier
if($_POST['panama']) {
$result = mysqli_query($con, "SELECT * FROM hats WHERE hat_cat='Panamas'");
} elseif($_POST['tophat']) {
$result = mysqli_query($con, "SELECT * FROM hats WHERE hat_cat='Tophats'");
} else {
$result = mysqli_query($con, "SELECT * FROM hats");
}
# ende neu
while($row = mysqli_fetch_array($result)){
echo "<img src=".$row['hat_name']." class='galery'>";
}
?>
而不是 如果($_POST['panama']){ 使用 if(isset($_POST['panama'])){ 应该可以解决
你需要使用函数isset()所以你的代码会变成下面这样;
if(isset($_POST['panama'])) {
$result = mysqli_query($con, "SELECT * FROM hats WHERE hat_cat='Panamas'");
} elseif(isset($_POST['tophat'])) {
$result = mysqli_query($con, "SELECT * FROM hats WHERE hat_cat='Tophats'");
} else {
$result = mysqli_query($con, "SELECT * FROM hats");
}
发生的事情是你的代码
$_POST['panama']
"Assumes"(如果你愿意的话),数组项 "panama" 总是会被传递给脚本,但在你的实例中情况并非如此,你要么传递;
<input type="image" name="panama" value="panama"/>
<input type="image" name="tophat" value="tophat"/>
因此使用 isset() 将检查该项目是否存在!