简体   繁体   中英

filter with clickable images (input) with php / mysql

I have a number of small images (input type=image), which I want to use as a filter, eg if I click on the first image, in the mysql query I only want to SELECT those with the corresponding value.

It seems to work but I get a PHP notice. What am I missing?

This is what I've got so far. I enclosed screen shots of the MySql notice and table structure.

MySQL通知 MySQL Hats表结构

 .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']." &nbsp; class='galery'>"; } ?> 

而不是if($ _ POST ['panama']){使用if(isset($ _ POST ['panama'])){

You need to use the function isset() so your code would become the following;

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"); 
}

What is happening is that your code

$_POST['panama']

"Assumes" (if you will), that the array item "panama" is always going to be passed to the script, but this is not the case in your instance you either pass;

<input type="image" name="panama" value="panama"/>
<input type="image" name="tophat" value="tophat"/>

So using isset() will check to see if the item exists or not!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM