I'm having a problem with my search engine!

  • Jack Sparrow
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Aug 31, 2006
  • Posts: 36
  • Status: Offline

Post March 1st, 2007, 9:01 am

Hi, I've been building a search engine for my works website which will enable people to search for articles that are stored in a mysql database. Now, I don't have alot of knowledge of php and mysql, but I've learned quite a bit over the last couple months which has enabled me to build the search engine.

So far I've got a working search that will bring in the articles based on what word is typed into the search box, and it will get it from the database and put it into a page. All tha works fine, there are just a few smaller things that I need sorted out before I can continue.

1) I would like to know how I could have a search box, that also functions as a drop down menu. It would have a list of categories, but you could also click in and type in whatever you like, kinda the way the Google tool bar works, but the categories would be preset, not words that you had already typed in. Is this possible to do.

2) Here's the other thing. I'm not sure how to explain this but I'll try. I've got a php script which does all the searching and brings in the links to the articles and lays them out on the page. My page has a table on it which is split into 2 columns, and the code is in the one on the left. Now, when I view the page in the browser it doesn't show the column on the right, it just deletes it altogether. So, I'm wondering if there's something in my php code that prevents it from showing the other column. The strange thing is that it will once the search has been done and the links to the articles come up, but not before. It also won't show up anything that is below the code, as in the same way as with the right hand column.

Here's my code so that if there's anything that would cause this it can be shown.


PHP Code:
$var = @$_GET['q'] ;
$trimmed = trim($var);

$host = "---";
$db_name = "---";
$db_user = "---";
$db_pass = "---";
$db_table = "articles";
mysql_connect($host, $db_user, $db_pass);
mysql_select_db($db_name);

$field_to_search = "header";
$field_to_search2 = "text";
$field_to_search3 = "category";
$query = "SELECT * FROM $db_table WHERE $field_to_search LIKE \"%$trimmed%\" OR
$field_to_search2 LIKE \"%$trimmed%\" OR $field_to_search3 LIKE \"%$trimmed%\" order by id";
$result = mysql_query($query);
$count = mysql_numrows($result);

if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
if ($numrows == 0)
{
echo "<h4>Articles</h4>";
echo "<p>Sorry, your search for " .$trimmed. " returned zero results</p>";
}
if (empty($s))
{
$s=0;
}
$result = mysql_query($query) or die("Couldn't execute query");
if($numrows > 1){ $return = "results";}
else{ $return = "result"; }

$count = 1 + $s ;

while ($r= mysql_fetch_array($result))
{
$id = $r["id"];
$header = $r["header"];
$text = $r["text"];
$image = $r["image"];
$excerpt = $r["excerpt"];

$count++ ;



?><a href="http://www.pwamm.com/articles.php?id=<? echo $id ?>"><br />
</a> <a href="http://www.pwamm.com/articles.php?id=<? echo $id ?>"><? echo $header ?></a><br />
<? echo $excerpt ?><br />
<? } ?>

This is the entire code, it's split up a bit at the end. If anyone knows if I could write this in a better way I'm open to improvements, but my 2 issues are crucial just now.

Thanks for any help.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 1st, 2007, 9:01 am

  • knexor2
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 27, 2006
  • Posts: 445
  • Loc: US
  • Status: Offline

Post March 1st, 2007, 7:46 pm

1) In terms of plain HTML, you can have a textbox or a dropdown field, but not both (unless I am sadly mistaken). However, I'm sure there is a DHTML solution (using HTML+JS) that would allow you to have this hybrid. If not, I guess I'd better get to work...

2) Not sure why this code would be messing with your table columns. What if you "don't have any results", does the right column show up then or not?

And yes, your code could be imporved in a few areas, but that's prob for a different discussion/topic ;)
"People can school you, but you must educate yourself." ~ John Taylor Gatto
Tech Knack Blog
The purpose of these forums is not to get an answer, but to learn an answer.
  • Jack Sparrow
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Aug 31, 2006
  • Posts: 36
  • Status: Offline

Post March 7th, 2007, 7:32 am

knexor2 wrote:
1) In terms of plain HTML, you can have a textbox or a dropdown field, but not both (unless I am sadly mistaken). However, I'm sure there is a DHTML solution (using HTML+JS) that would allow you to have this hybrid. If not, I guess I'd better get to work...

2) Not sure why this code would be messing with your table columns. What if you "don't have any results", does the right column show up then or not?

And yes, your code could be imporved in a few areas, but that's prob for a different discussion/topic ;)


Heres the link to the page, so that you can see what if going on.

http://www.pwamm.com/articlelinks.php

I discovered that everything that is below the php code will not show up, until a search has been made and results are shown, as you can see for yourself.

And what do you reccomend I should change to clean up the code?
  • knexor2
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 27, 2006
  • Posts: 445
  • Loc: US
  • Status: Offline

Post March 7th, 2007, 7:47 am

Ok, when there is no search term, the content after the "Please enter a search term..." <p> is not getting sent to the screen.

if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}

Remove these exit;s, pronto. This is saying, "If the trimmed variable is empty or there is no search var, exit the processing of this page", which effectively cuts the page off.

You don't hardly ever want to use "exit" in a php script unless you have a good reason to. For example, I am writing a simple guestbook script for my site, and I am utilizing AJAX for processing the submitted form, amongst other things. The AJAX is loaded on the index.php page, which is the same page that the form is both loaded from and action'd to. This means that this page also handles the processing of $_POST data, so that in the event that the user has disabled JS, my script still works. I have the AJAX set up to call index.php with the POST data, but I don't want the entire script, just a success or error message. Therefore, I added a $_GET var called process; if this var is set, I echo either "success" or "error[tab]error msg", then immediately exit.
"People can school you, but you must educate yourself." ~ John Taylor Gatto
Tech Knack Blog
The purpose of these forums is not to get an answer, but to learn an answer.
  • Jack Sparrow
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Aug 31, 2006
  • Posts: 36
  • Status: Offline

Post March 8th, 2007, 2:43 am

Ok, I've deleted the exits, but now when the page loads it brings in everything that is in the database and displays 'We don't seem to have a search parameter' although it searches fine. Is there something in my code that I need to change so that it does have a search parameter and so that the results don't come in until a search has been made. I'm new to php so there may something in my code that shouldn't be there.

Thanks. :)
  • knexor2
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 27, 2006
  • Posts: 445
  • Loc: US
  • Status: Offline

Post March 8th, 2007, 5:14 am

It may be the @ in

$var = @$_GET['q'];

@ suppresses error messages when used before a function, but I'm not sure what it would do when used before a variable.

If removing that doesn't work, go through the if-else statements in your head, make sure everything makes sense ;)
"People can school you, but you must educate yourself." ~ John Taylor Gatto
Tech Knack Blog
The purpose of these forums is not to get an answer, but to learn an answer.
  • Jack Sparrow
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Aug 31, 2006
  • Posts: 36
  • Status: Offline

Post March 8th, 2007, 5:21 am

I deleted the @ and it doesn't seem to have affected anything, makes me wonder why I needed it. I'll check the other things just now.
  • knexor2
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 27, 2006
  • Posts: 445
  • Loc: US
  • Status: Offline

Post March 8th, 2007, 5:22 am

Remove the @ before you $_GET assignment.
"People can school you, but you must educate yourself." ~ John Taylor Gatto
Tech Knack Blog
The purpose of these forums is not to get an answer, but to learn an answer.
  • Jack Sparrow
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Aug 31, 2006
  • Posts: 36
  • Status: Offline

Post March 8th, 2007, 5:24 am

LOL, I wasn't quick enough in editing my post. I'll check the if-else statements.
  • Jack Sparrow
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Aug 31, 2006
  • Posts: 36
  • Status: Offline

Post March 8th, 2007, 5:31 am

Everything seems in order. The problem is that the contents of the database are coming in, without any search being made, they just appear on the page as though a search had been made.

Here's my edited coed, in case I missed something:

<?
$var = $_GET['q'] ;
$trimmed = trim($var);

$host = "---";
$db_name = "---";
$db_user = "---";
$db_pass = "---";
$db_table = "articles";

mysql_connect($host, $db_user, $db_pass);
mysql_select_db($db_name);

$field_to_search = "header";
$field_to_search2 = "text";
$field_to_search3 = "category";
$query = "SELECT * FROM $db_table WHERE $field_to_search LIKE \"%$trimmed%\" OR
$field_to_search2 LIKE \"%$trimmed%\" OR $field_to_search3 LIKE \"%$trimmed%\" order by id";

$result = mysql_query($query);
$count = mysql_num_rows($result);
?>

<form action="<?=$PHP_SELF?>" method="GET" name="search" id="search">
Search for an article by name :
<input type="text" name="q" />
<input type="submit" name="search" value="Search" />
</form>
<form>
or category:
<select name='q'>
<option value="Billboard">Billboard</option>
<option value="Evangelism">Evangelism</option>
<option value="Tent">Tent</option>
</select>
<input type="submit" name="search" value="Search" />
</form>

<?
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
}

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

if ($numrows == 0)
{
echo "<h4>Articles</h4>";
echo "<p>Sorry, your search: " . $trimmed . " returned zero results</p>";
}
if (empty($s))
{
$s=0;
}

$result = mysql_query($query) or die("Couldn't execute query");
if($numrows > 1){ $return = "results";}
else{ $return = "result"; }

$count = 1 + $s;

while ($r= mysql_fetch_assoc($result))
{
$id = $r["id"];
$header = $r["header"];
$image = $r["image"];
$excerpt = $r["excerpt"];

$count++;
?>
  • knexor2
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 27, 2006
  • Posts: 445
  • Loc: US
  • Status: Offline

Post March 8th, 2007, 6:50 am

if ($trimmed == "")

{

echo "<p>Please enter a search...</p>";

}

$numresults=mysql_query($query);

$numrows=mysql_num_rows($numresults);

You need an else here. You're saying here that is the search term ($trimmed) is blank, echo an error and search for articles with fields like ""...which, I think, would be all of them. Add an else to the if that contains the entire query-print section.
"People can school you, but you must educate yourself." ~ John Taylor Gatto
Tech Knack Blog
The purpose of these forums is not to get an answer, but to learn an answer.
  • Jack Sparrow
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Aug 31, 2006
  • Posts: 36
  • Status: Offline

Post March 8th, 2007, 9:13 am

Right, ok. How would I write that. Would I just add it in after the echo?
  • knexor2
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 27, 2006
  • Posts: 445
  • Loc: US
  • Status: Offline

Post March 8th, 2007, 10:38 am

if ($trimmed == "")

{

echo "<p>Please enter a search...</p>";

}
else {
$numresults=mysql_query($query);

$numrows=mysql_num_rows($numresults);


.......all other processing code that is used to output the article list.........
}
"People can school you, but you must educate yourself." ~ John Taylor Gatto
Tech Knack Blog
The purpose of these forums is not to get an answer, but to learn an answer.
  • hrishidev
  • Novice
  • Novice
  • No Avatar
  • Joined: Oct 08, 2006
  • Posts: 25
  • Status: Offline

Post March 12th, 2007, 8:53 am

just one thing i want to suggest...verify all user input are in proper format before appending into sql query
also take care of sql injections too ....

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 138 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2010 Unmelted, LLC. Driven by phpBB © 2010 phpBB Group.