You are here: Re: search queries « PHP Programming Language « IT news, forums, messages
Re: search queries

Posted by Erwin Moller on 02/09/07 14:49

caine wrote:

> I'm doing a search application for my project. My code can prompt
> alert popup window when the user doesn't key in any keywords. However,
> if the user keys in any keywords, it juz return "Please enter
> keywords!" whatever it is. I had set proper looping for suitable
> condition checking, but it is still generate the same problem.

Sorry to say Caine, but your code is a mess.
You just don't throw code like that to a group and expect us to unweave it.
Bad nettiquette.

A friendly word of advise: You might consider to clean it up before posting
here in future....
We REALLY don't care about the stylesheet, the databasequeries (in this
case), your HTML-markup, string decoding, etc.
It is just noise.
You even commented out if-statements, making the spaghetti even worse (for
you and for us) to unravel.


I made a few comments though:

>
> <html>
> <head>
> <title>Searching Results</title>
> </head>
> <body background="abstract.gif">
>
> <link rel="STYLESHEET" type="text/css" href="stylesheet.css">
>
> <?php
> echo "<h1>Searching Results</h1>";
>
> //perform search
> if($_POST['submit'] == "")

What is this excactly doing in your opinion?
You are checking if the POST contains a name/value pair that goes by the
name 'submit' and contains an empty string.
Is that what you WANT to check?

I expect you want to check IF the form has been posted.
Try something that actually tests that, like:

[assuming your submitbutton exists in your form]
if (isset($_POST["submit"])){
// form received
} else {
// no form received
}



> {
>
> echo "post";


So this part of the script will only be reached if you posted a form with
method POST, containing a 'submit' that is an empty string.

>
> //if(isset($_POST['submit']))
> //{
> $db = mysql_connect("localhost", "root", "") or die(mysql_error());
>
> mysql_select_db("bulletin", $db) or die(mysql_error());
>
> $searchWords = explode(" ", $_POST['textfield']);
>
> foreach ($searchWords as $word)
> {
> if ($word)
> {
> $whereParts[] = "`TITLE` LIKE '%{$word}%' OR `DEPARTMENT` LIKE '%
> {$word}%'" ;
> }
> }
>
> $whereClause = implode(" OR ", $whereParts);
>
> $query = "SELECT * FROM `bul_data` WHERE {$whereClause} ORDER by
> `DATE`";
>
> $res = mysql_query($query) or die(mysql_error());
>
> echo "query=".$query;
>
> if(mysql_num_rows($res)>0)
> {
>
> echo "<table width=\"100%\" border=\"1\">";
> echo "<tr>";
> echo "<th class=\"normal\">Date</th>";
> echo "<th class=\"normal\">Title</th>";
> echo "<th class=\"normal\">Department</th>";
> echo "<th class=\"normal\">Link</th>";
> echo "</tr>";
>
> while($row=mysql_fetch_assoc($res))
> {
> $date = $row['DATE'];
> $date = decode($date);
>
> $title = $row['TITLE'];
> $title = decode($title);
>
> $department = $row['DEPARTMENT'];
> $department = decode($department);
>
> $link = $row['LINK'];
> $link = decode($link);
>
>
>
> echo "<tr>";
> echo "<td><strong>$date</strong></td>";
> echo "<td><strong>$title</strong></td>";
> echo "<td>$department</td>";
> echo "<td><a href = \"$link\">$link</a></td>";
> echo "</tr>";
>
>
> }
>
> echo "</table>";
>
> }
>
> //}
> }
>
>
>
> else
> {


And here we are when the form did not contain a 'submit' with value empty
string.


> echo "38292930";
> echo "Please enter keywords!";
> //echo "<script language=\"JavaScript\">
> //<!--
> //alert('Please enter keywords!');
> //-->
> //</script>";
> }


End of if-statement.

<snipped the rest of the script>


Since we can only guess what is actually in your form, I cannot say with
certainty this is the problem, but it sounds likely.

Also, if you are in doubt WHAT is excactly pased to PHP by some form, just
do this:
echo "<pre>";
print_r($_POST);
echo "<pre>";

Regards,
Erwin Moller

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация