[英]Undefined Index in PHP $_POST Array
我有一個頁面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>";
}
}
}
}
?>
好像你在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
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.