Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Is this possible

hey here is my query . It allow user to retreive date by searching by student name or class but when i make textbox with name empty and select the class the complete data shown to user instead of data with respective class.

function search_details($name = null,$class = null){
    //database connected through connection.php
     include "connection.php";
      try{
        $search = $db->prepare("
                   Select first_Name,last_Name,class,city 
                   FROM details 
                   WHERE first_name LIKE ? || class = ?"
                );     
        $search->bindValue(1,$name."%",PDO::PARAM_STR);
        $search->bindValue(2,$class,PDO::PARAM_INT);
     }catch(Exception $e){
        echo "Not Retreived";
     }
       $search->execute();
       $search = $search->fetchAll(PDO::FETCH_ASSOC);
       return $search;
  }  
   if(isset($_GET['s'])|| isset($_GET['class'])){
      $name = filter_input(INPUT_GET,"s",FILTER_SANITIZE_STRING);
      $class = filter_input(INPUT_GET,"class",FILTER_SANITIZE_NUMBER_INT);
      if(empty($name) && empty($class)){
         header("location:index.php");
         exit;
      }
      $search = search_details($name,$class);
   }

1 Answer

Simon Coates
Simon Coates
28,694 Points

would an empty name result in:

where name like "%"

? if so, this would always be true, i think.