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);
}
?>
答案 0 :(得分:0)
POST键区分大小写。它必须是if(isset($_POST['Submit']))
$_GET['Submit']
。
答案 1 :(得分:0)
为什么不检查搜索输入中的数据是否存在? (由评论请求重新制定: - ))
// Get the search variable from URL
if(isset($_GET['q']))
而不是
// Get the search variable from URL
if(isset($_POST['submit']))
答案 2 :(得分:0)
您正在Submit
中检查$_POST
,但表单中指定的方法为GET
。
要使其正常工作,请更改PHP代码以查找$_GET['Submit']
。
(但我会将所有内容,包括形式方法,转移到POST
)。
也就是说,您的脚本似乎已粘贴两次,第二个版本使用$q
而不是$trimmed
(定义为$q
?),以及一个名为'table'的SQL表而不是'问题':
// 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");