search queries

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.

<title>Searching Results</title>
<body background="abstract.gif">

<link rel="STYLESHEET" type="text/css" href="stylesheet.css">


    echo "<h1>Searching Results</h1>";

//perform search
if($_POST['submit'] == "")

    echo "post";

    $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 '%%' OR `DEPARTMENT` LIKE '%
%'" ;

    $whereClause = implode(" OR ", $whereParts);

    $query = "SELECT * FROM `bul_data` WHERE ORDER by

    $res = mysql_query($query) or die(mysql_error());

    echo "query=".$query;


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

            $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>";



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

//function decode($string)
//    $string = str_replace("&amp;", "&", $string);
//    $string = str_replace("&lt;", "<", $string);
//    $string = str_replace("&gt;", ">", $string);
//    $string = str_replace("&#039;", "'", $string);
//    $string = str_replace("&quot;", "\", $string);
//    return $string;


function decode($text)
    $text =  html_entity_decode($text,ENT_QUOTES);
    return $text;



<table width ="100%" border ="0" cellspacing ="1" cellpadding ="2">
    <td class="normal" width ="10%" align ="center"><a href =
"homepg.php">Home&nbsp;&nbsp;|&nbsp;&nbsp;</a><a href =
"search.php">Search Again?</a></td>



Re: search queries

caine wrote:

Quoted text here. Click to load it

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:

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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>";
echo "<pre>";

Erwin Moller

Re: search queries

caine wrote:
Quoted text here. Click to load it

After real quick look, it seems that first condition
  if($_POST['submit'] == "")
fails and that's why it goes to
Please enter keywords -part.

Is the method of your previous form post or get?
Is the name of the submit button "submit" for sure?

Site Timeline