search.php 页面只是没有获取结果。只要我按下提交,无论我输入什么字符串,我都会得到一个空白页。无法理解为什么!请帮忙
表单工作正常,但不知何故在提交时给了我一个空白页,而不是结果,我哪里出错了?
form code
<form name="form" action="../search.php" method="get">
<input name="q" type="text" value="type!" onfocus="this.value=''" />
<input type="submit" name="Submit" value="Search" />
</form>
php code
<?php
// Get the search variable from URL
if(isset($_POST['submit']))
{
$var = $_GET['q'] ;
$trimmed = trim($var);//trim whitespace from the stored variable
// rows to return
// check for an empty string and display a message.
if ($trimmed=="")
{
echo "<p>Please enter a search...</p>";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to database
$con=mysql_connect("localhost","",""); //(host, username, password)
//specify database
mysql_select_db("test",$con) or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from questions where question like \"%$trimmed%\" or detail like \"% $trimmed%\" or name like \"%$trimmed%\"
order by name"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query = "SELECT * FROM table WHERE question LIKE '%$q%' OR name LIKE '%$q%' or detail like '%$q%'";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// display the results returned
while ($row= mysql_fetch_array($result)) {
echo "<table align=center class=lims>";
echo "<tr>";
echo "<th bgcolor=#5D9BCC >QUESTIONS</th>";
echo "<td bgcolor=#FEE9A9>" . $row['question'] . "</td>";
echo "<tr>";
echo "<th bgcolor=#5D9BCC>Alias</th>";
echo "<td bgcolor=#FEE9A9>" . $row['detail'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th bgcolor=#5D9BCC>Code</th>";
echo "<td bgcolor=#FEE9A9>" . $row['name'] . "</td>";
echo "</tr>";
$title = $row["name"];
echo "$count.) $title" ;
$count++ ;
}
$currPage = (($s/10) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/10);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%10) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/10)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+10;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + (10) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
mysql_close($con);
}
?>