Jump to content

search error


caine

Recommended Posts

I want to perform searching queries once user enters searching keywords. However, there is no display returned after that. What will be the problem?
[code]
<html>
<head>
<title>Searching Results</title>
</head>
<body>

<?php

$db = mysql_connect("localhost", "root", "xxxx") or die(mysql_error());

mysql_select_db("test", $db) or die(mysql_error());

$display ="<h1>Searching Results</h1>";


//perform search
if($search == $_POST['Search'])
{
$qry = mysql_query('SELECT * from test where TITLE, DEPARTMENT like "%'.$search.'%" ORDER by DEPARTMENT');

    if(mysql_num_rows($qry)< 1)
    {
$display = "<p>Sorry, no matching results.</p>";
    }

    else
    {



while ($results = mysql_fetch_array($qry))
{

echo "<table width="100%" border="0">";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>Department</th>";
echo "<th>Link</th>";
echo "</tr>";


$title = $results['title'];
$department = $results['department'];
$campus = $results['campus'];
$link = $results['link'];

$display .= "<tr>
        <td><strong>$title</strong></td>
    <td>$department</td>
    <td><a href="$link"></a></td>
    </tr>";
}//while

$display .="</table>";


    }//else $qry
}//if $search

else
{
echo "No matches.";
}


?>
</body>
</html>[/code]

Parsing error with this part:
echo "<table width="100%" border="0">";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>Department</th>";
echo "<th>Link</th>";
echo "</tr>";
Link to comment
Share on other sites

I had modified the code into the following, but whatever keywords that I typed, the keywords ,$_POST['Search'] cannot be parsed into the search results page when I debug. Therefore, there's no searching results for the data in db. The error statement is : Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Program Files\xampp\htdocs\search_results_new.php on line 19. What is the problem?

The form is like this:
[code]<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search</title>
</head>
<body>
<table width="766" border="1">
  <tr>
    <td width="1530"><h1>Search Bulletin.... </h1></td>
  </tr>
  <tr>
    <td><label>
      <div align="center">Keyword:
<form action="search_results_new.php" method="post">
<input name="textfield" type="text" />
        <input type="submit" name="submit" value="Search" />
</form>
      </div>
    </label>
      <label>
      <div align="center"></div>
      </label></td>
  </tr>
  <tr>
    <td><a href="homepg.php">Back to main menu.</a></td>
  </tr>
</table>
</body>
</html>[/code]

The search results page is like this:
[code]<html>
<head>
<title>Searching Results</title>
</head>
<body>

<?php

$db = mysql_connect("localhost", "root", "xxxx") or die(mysql_error());

mysql_select_db("bulletin", $db) or die(mysql_error());

$display ="<h1>Searching Results</h1>";


//perform search
if(isset($_POST['Search']))
{
$res = mysql_query("SELECT * FROM `bul_data` WHERE `TITLE`  LIKE '%$_POST['Search']%' OR `DEPARTMENT` LIKE '%$_POST['Search']%' ORDER by DATE", $db);
       
echo "res=".$res;

if(mysql_num_rows($reS)>0)   
            {
                //your table header goes here...
                echo "<table width=\"100%\" border=\"0\">";
                echo "<tr>";
                echo "<th>Title</th>";
                echo "<th>Department</th>";
                echo "<th>Link</th>";
                echo "</tr>";
       
                while($row=mysql_fetch_assoc($res))
                {
                    $title = $results['title'];
                    $department = $results['department'];
                    $campus = $results['campus'];
                    $link = $results['link'];

                    $display .= "<tr>
                                <td><strong>$title</strong></td>
                                <td>$department</td>
                                <td><a href=$link></a></td>
                      </tr>";
}

$display .="</table>";


    }
}

else
{
echo "No matches.";
}


?>
</body>
</html>[/code]
Link to comment
Share on other sites

you cannot use $_POST[] variables in your query like this.
change this:
[code=php:0]$res = mysql_query("SELECT * FROM `bul_data` WHERE `TITLE`  LIKE '%$_POST['Search']%' OR `DEPARTMENT` LIKE '%$_POST['Search']%' ORDER by DATE", $db);[/code]

to this:
[code=php:0]$res = mysql_query("SELECT * FROM `bul_data` WHERE `TITLE`  LIKE '%{$_POST['Search']}%' OR `DEPARTMENT` LIKE '%{$_POST['Search']}%' ORDER by DATE", $db);[/code]

and it should work just dandy.
Link to comment
Share on other sites

Thanks ProjectFear, I had corrected the syntax as you said. However, there's still no data returned after entering the keywords. There's no more error statements, instead it shows this only :

$query = SELECT * FROM `bul_data` WHERE `TITLE`  LIKE '%Submit%' OR `DEPARTMENT` LIKE '%Submit%' ORDER by DATE

Could there be any other possible problems occurred from the form program?
Link to comment
Share on other sites

Ok, I removed that line. Actually discovered that there's no <?php and ?> at the form program, so I added them in. Besides, I had corrected the syntax as mjdamato said. But there's still no results returned. What are other possibilities for the failure?

I think I better postup latest updated program. Here they are:
[code]
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search</title>
</head>
<body>
<?php

echo "<h1>Search Bulletin </h1>";
echo "<h3>Keywords:</h3>";
echo "<form action=\"search_results_new.php\" method=\"post\">";
echo "<input name=\"Submit\" type=\"text\">";
        echo "<input type=\"Submit\" name=\"Submit\" value=\"Submit\">";
echo "</form>";
echo "<a href=\"homepg.php\">Back to main menu.</a>";
 
?>
</body>
</html>[/code]

The search results page :
[code]<html>
<head>
<title>Searching Results</title>
</head>
<body>

<?php

$db = mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("bulletin", $db) or die(mysql_error());

$display ="<h1>Searching Results</h1>";


//perform search
if(isset($_POST[Submit]))
{
$query = "SELECT * FROM bul_data WHERE `TITLE`  LIKE '%{$_POST[Submit]}%' OR `DEPARTMENT` LIKE '%{$_POST[Submit]}%' ORDER by DATE";

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

echo "qry=".$query;
       
if(mysql_num_rows($res)>0)   
        {

                $display.= "<table width=\"100%\" border=\"0\">";
                  "<tr>";
                  "<th>Title</th>";
                  "<th>Department</th>";
                  "<th>Link</th>";
                "</tr>";
       
                while($row=mysql_fetch_assoc($res))
                {
                    $title = $results['title'];
                    $department = $results['department'];
                    $campus = $results['campus'];
                    $link = $results['link'];

                    $display .= "<tr>
                                <td><strong>$title</strong></td>
                                <td>$department</td>
                                <td><a href=$link></a></td>
                      </tr>";
}

$display .="</table>";



  }
echo $display;
}

else
{
echo "No matches.";
}


?>
</body>
</html>
[/code]
Link to comment
Share on other sites

You changed the name of the text box to submit, plus you have a submit button with the name submit. Now you have created an array!

Change the name of the textbox to something more relevant like "textSearch" - I would advise against ever giving a filed the name submit which is not a submit button.

Then change the $_POST[Submit] in your query to the name you hcanged the text box to.
Link to comment
Share on other sites

$searchWords = explode(" ", $_POST[Submit]);

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";
Link to comment
Share on other sites

  • 2 weeks later...
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.