dannyp100 Posted April 15, 2013 Share Posted April 15, 2013 I am having difficulty getting my search working for the page that i am doing: heres my search function within a class called CourseService public function search() { $searchterm = $_POST['searchterm']; $sql = "SELECT * FROM srs_course WHERE coursetitle LIKE '% :searchterm %' "; try { $db = dbConnection::getConnection(); $query = $db->prepare($sql); $query->bindParam(':searchterm', PDO::PARAM_STR); $query->execute(); if(!$query->rowCount()==0) { while($row = $query->fetch()) { echo "<tr>"; echo "<td>".$row['coursecode']."</td>"; echo "<td>".$row['coursetitle']."</td>";; } } else { echo "No results found!"; } $query->closeCursor(); } catch (Exception $ex){ echo "Something went wrong " . $ex; } } Then I will call the function in my servicepipeclass: case 'search': $s = new CourseService(); $st = $s->search(); echo $st; break; The finally on a php page called phptesting.php went user types something it, it should bring back search results but it doesnt: <form action="courseservicepipe.php" method="post"> Search: <input type="text" name="searchterm" /><br /> <input type="submit" name="submit" value="Submit" /> </form> I would also like help on how to change my code to search all table in the database and for keywords to anything, these are the tables: srs_student, srs_course, srs_student_module Any help would be amazing! I'm extremely confused Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 15, 2013 Share Posted April 15, 2013 You aren't binding ":searchterm" to anything. Try changing these three lines: $searchterm = '%'.$_POST['searchterm'].'%'; $sql = "SELECT * FROM srs_course WHERE coursetitle LIKE :searchterm "; $query->bindParam(':searchterm', $searchterm, PDO::PARAM_STR); http://php.net/manual/en/pdostatement.bindparam.php Quote Link to comment Share on other sites More sharing options...
dannyp100 Posted April 15, 2013 Author Share Posted April 15, 2013 Still doesnt seem to be working, just giving me an error saying theres an undefined index 'searchterm'. I'm not sure if my code in my service pipe is right or the form. Also its not bringing me back any search results, saying 'No results found', how can i search throughout multiple tables and return the results as just a field and not under any headings? Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 15, 2013 Share Posted April 15, 2013 I don't see how any of this code would output that error. I think you may have forgotten a colon ( somewhere. Could you post your updated code that you are now running? Quote Link to comment Share on other sites More sharing options...
dannyp100 Posted April 15, 2013 Author Share Posted April 15, 2013 search function within CourseService Class public function search() { $searchterm = '%'.$_POST['searchterm'].'%'; $sql = "SELECT * FROM srs_course WHERE coursetitle LIKE :searchterm "; try { $db = dbConnection::getConnection(); $query = $db->prepare($sql); $query->bindParam(':searchterm', $searchterm, PDO::PARAM_STR); $query->execute(); if(!$query->rowCount()==0) { while($row = $query->fetch()) { echo "<tr>"; echo "<td>".$row."</td>"; } } else { echo "No results found!"; } $query->closeCursor(); } catch (Exception $ex){ echo "Something went wrong " . $ex; } } My service pipe class code: case 'search': $s = new CourseService(); $st = $s->search(); echo $st; break; I check to see if its working by the url, adding &searchterm=b, but no results present. How can i do this via a search form so it picks up what the user has inputted? http://localhost/courseservicepipe.php?action=search&searchterm=b Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 15, 2013 Share Posted April 15, 2013 Oh, I read the error wrong. I read "unknown column" for some silly reason. If you want the search term to be passed through the URL, simply change the form method to "get" and the variables from $_POST to $_GET. I think the disconnection might be at your switch case. What variable are you switching to test for "search?" Quote Link to comment Share on other sites More sharing options...
dannyp100 Posted April 15, 2013 Author Share Posted April 15, 2013 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'error' ; switch ($action) { case 'search': $s = new CourseService(); $st = $s->search(); echo $st; break; The search is not bringing up any results so i'm guessing its not searching properly: Thats the url i am testing the search on: http://localhost/courseservicepipe.php?action=search&searchterm=accounting comes back with 'No Results found' so confused! Quote Link to comment Share on other sites More sharing options...
godleydesign Posted April 15, 2013 Share Posted April 15, 2013 You will most likely want to pass the search term into the function, then when you call the function you can pass the POST variable - allows you to reuse the code throughout the site. example:public function search($searchterm) Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 15, 2013 Share Posted April 15, 2013 I agree with godleydesign on the structure change. I think you are getting close! Are you sure that your table contains that data? Try running the query as it would be in phpMyAdmin or a similar utility. SELECT * FROM srs_course WHERE coursetitle LIKE '%accounting%' If you get results from that, you might consider this warning from php.net: If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications. http://php.net/manual/en/pdostatement.rowcount.php See if that query works first, though Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.