0

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: &quot;" . $trimmed . "&quot; 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: &quot;" . $var . "&quot;</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.)&nbsp;$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 "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
  Prev 10</a>&nbsp&nbsp;";
  }

  // 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 "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</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);
}
?>
4

3 に答える 3

0

検索入力からのデータが存在するかどうかを確認してみませんか?(コメントリクエストで再定式化:-))

// Get the search variable from URL
if(isset($_GET['q']))

それ以外の

// Get the search variable from URL
if(isset($_POST['submit']))
于 2012-09-30T22:25:14.407 に答える
0

でチェックしてSubmit$_POSTますが、フォームで指定されたメソッドはですGET

これを機能させるには、PHPコードを変更してを探します$_GET['Submit']

(ただし、代わりに、フォームメソッドを含むすべてのものをに移動しますPOST)。

そうは言っても、スクリプトは2回貼り付けられているようです。2番目のバージョン$qでは$trimmed$q定義されていますか?)の代わりにを使用し、「質問」の代わりに「テーブル」と呼ばれる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");
于 2012-09-30T22:26:35.953 に答える
0

POSTキーでは大文字と小文字が区別されます。それはする必要がありますif(isset($_POST['Submit']))

編集:上記の答えを気にしないでください。問題は、formメソッドがGETであり、$_POSTを使用して送信キーにアクセスしようとしていることです。に変更する必要があります$_GET['Submit']

于 2012-09-30T22:18:43.600 に答える