2015-12-24 28 views
3

我有一個index.php頁面,我添加了一個與數據庫連接的搜索表單。因此,當用戶搜索任何單詞時,結果將顯示在數據庫中。一旦在頁面上生成結果,我希望用戶將結果導出到.doc文件中。

我只想將查詢結果導入到.doc文件中,但由於某些原因我得到一個空白的.doc文件。

這裏是我的代碼:

searchform:

<form id="searchform" method="post"> 
    <input type="text" name="searchit" id="searchit" class="txt" /> 
    <input type="submit" value="Search" id="button" class="button" /> 
</form> 

查詢:

<?php 
include("database.php"); 
    $term = strip_tags(substr($_POST['searchit'],0, 100)); 
    $term = $mysqli->real_escape_string($term); 

    if($term=="") { 
    echo "<div class='error'>Enter Something to search</div>"; 
    exit(); 
    } 

    termcheck($term, $mysqli);    
    function termcheck($term, $mysqli){ 
    $qry="Select * from pogel where title = '$term'"; 
    if($result = $mysqli->query($qry)){ 

     $num_rows = $result->num_rows; 
     if($num_rows > 0) { 

      while($row = $result->fetch_assoc()) 
      { 
       echo "Stem : ".$row['title']."<br>"; 
      } 

     } 
} 
} 
?> 
+1

[PHP的可能重複: 「請注意:未定義的變量」 和「注意:未定義的索引「](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – FirstOne

+1

您的HTML頁面中是否有名稱爲」searchit「的元素哪些發送POST數據? – Phiter

+0

是的,'searchhit'已經在我的搜索頁面上,但仍然出現錯誤。 – Mehreen

回答

1

好像你只是沒有在您的文章這個關鍵。

你可以試試這個,如果沒有這樣的元素來腳本:

$searchit = filter_input(INPUT_POST, 'searchit'); 

if(!$searchit) { 
    echo "<div class='error'>Enter Something to search</div>"; 
    exit(); 
} 

$term = strip_tags(substr($searchit,0, 100)); 
$term = $mysqli->real_escape_string($term); 

嘗試使用此方法生成的DOC文件:

<?php 
include("database.php"); 
$term = strip_tags(substr($_POST['searchit'],0, 100)); 
$term = $mysqli->real_escape_string($term); 

if($term=="") { 
    echo "<div class='error'>Enter Something to search</div>"; 
    exit(); 
} 

$output = termcheck($term, $mysqli); 

function termcheck($term, $mysqli){ 
    $qry = "Select * from pogel where title = '$term'"; 
    $result = ''; 

    if($result = $mysqli->query($qry)){ 
     $num_rows = $result->num_rows; 
     if($num_rows > 0) { 
      while($row = $result->fetch_assoc()) { 
       $result .= "Stem : ".$row['title']."<br>"; 
      } 
     } 
    } 

    return $result; 
} 

ob_flush(); 

header("Content-type: application/vnd.ms-word"); 
header("Content-Disposition: attachment;Filename=document_name.doc"); 

echo $output; 

//ob_flush_clean() might be useful here, but not sure 
?> 
+0

您的代碼修正了該錯誤,但不幸的是,我收到一個空白頁面,並且查詢沒有發佈任何結果。 – Mehreen

+0

您能否使用提交'searchit'的表單更新您的問題?順便說一句,嘗試'var_dump'ing $ _POST數組來查看是否有'searchit'鍵存在。 –

+0

剛剛更新了我的問題和代碼。請再檢查一次! – Mehreen