0

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 &gt;

        $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;
    }}
?>
6
  • 7
    Replace all your == in your queries with a single = Commented Apr 9, 2014 at 12:35
  • In PHP , 1 '=' means assignment, and 2 '==' means validation, checking if a value is the same as another1. In MYSQL its only 1 '=', like Fred said Commented Apr 9, 2014 at 12:37
  • I changed all "==" with "=" in query but problems is same control goes to blank page. Commented Apr 9, 2014 at 12:42
  • All the more reasons why I didn't make it an "answer" ;-) I didn't want to open up that infamous can of "you know what". It's debugging time. Commented Apr 9, 2014 at 12:43
  • In this line "echo '<img src=data:image/png;base64,' . base64_encode($rows['16_16']).' />';" where $rows is coming from ? I think it should be $results Commented Apr 9, 2014 at 12:53

2 Answers 2

1

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 &gt;

       $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;
  }
}

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome Ahsan !!! Hope you have noticed the differences in the code, so have an idea of the mistake you was doing
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.