Jump to content

PHP Search Facility


dannyp100

Recommended Posts

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?"

Link to comment
Share on other sites

$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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.