Reputation: 5
I am new learner of php, i am developing a website. i creating a search form that will allow users to select from a drop list the image type and cost than the system will search in the database and select the appropriate images and display it on the screen. but the problem is that no images are displaying can anyone help me. my code is below
<?php
$type=$_POST['type'];
$cost=$_POST['cost'];
$search=$_POST['search'];
if (isset($_POST['submit'])) {
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($search) >= $min_length){ // if query length is more or equal minimum length then
$search = htmlspecialchars($search);
// changes characters used in html to their equivalents, for example: < to >
$search = mysql_real_escape_string($search);
if($type="colored" AND $cost="free"){
$query="SELECT * FROM albums WHERE type='colored' and price=0 and title LIKE '%".$search."%'";
}
elseif ($type="bw" AND $cost="free") {
$query="SELECT * FROM albums WHERE type ='bw' and price =0 and title LIKE '%".$search."%'";
}
elseif ($type="both" AND $cost="free") {
$query="SELECT * FROM albums WHERE type ='colored' or 'bw' and price =0 and title LIKE '%".$search."%'";
}
if(mysql_num_rows($query) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($query)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo '<img src=data:image/png;base64,' . base64_encode($rows['16_16']).' />';
}
}
else{ // if there is no matching rows do following
echo "No results";
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}}
?>
Upvotes: 0
Views: 78
Reputation: 143
Please use the following code to fix your issue.
<?php
$type=$_POST['type'];
$cost=$_POST['cost'];
$search=$_POST['search'];
if (isset($_POST['submit'])) {
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($search) >= $min_length) {
// if query length is more or equal minimum length then
$search = htmlspecialchars($search);
// changes characters used in html to their equivalents, for example: < to >
$search = mysql_real_escape_string($search);
if ($type=="colored" AND $cost=="free") {
$query = "SELECT * FROM albums WHERE type = 'colored' and price = 0 and title LIKE '%".$search."%'";
} elseif ($type=="bw" AND $cost=="free") {
$query="SELECT * FROM albums WHERE type = 'bw' and price = 0 and title LIKE '%".$search."%'";
} elseif ($type=="both" AND $cost=="free") {
$query="SELECT * FROM albums WHERE ( type ='colored' or 'bw' ) and price = 0 and title LIKE '%".$search."%'";
}
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){ // if one or more rows are returned do following
while($rows = mysql_fetch_array($result)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo '<img src=data:image/png;base64,' . base64_encode($rows['16_16']).' />';
}
} else { // if there is no matching rows do following
echo "No results";
}
} else { // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
}
?>
Upvotes: 1
Reputation: 599
You should use single = in SQL.
In php = is for declaring a variable like
$x = 1;
and == is for comparing values like
$test = ($x == true)? true:false;
(returns true)
and === is for comparing type and value, so:
$res1 = ($x === 1) ? true:false;
$res2 = ($x === true) ? true:false;
res1 = true, res2 = false
Upvotes: 0