PHP $ _POST数组

时间:2015-12-24 14:00:25

标签: php mysql .doc

我有一个页面index.php,我添加了一个与数据库连接的搜索表单。这样,当用户搜索任何单词时,结果将显示在数据库中。在页面上生成结果后,我希望用户将结果导出到.doc文件中。

我只想将查询结果放入.doc文件中,但出于某些原因我得到一个空白的.doc文件。

以下是我的代码:

搜索表单:

<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 个答案:

答案 0 :(得分:1)

好像你在POST中没有这把钥匙。

如果剧本没有这样的元素,你可以试试这个:

$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
?>