我試圖在我的網站上做一個搜索欄,用來查詢mysql數據庫並以一種時尚的方式返回結果。搜索將用於查找名稱。例如,用戶可以輸入「John Doe」,並且關於John Doe的任何信息將被返回。不幸的是,每當我點擊提交按鈕,我的網站轉換到'search.php'頁面是完全空白的。我測試了與數據庫的連接並知道它正在工作。我的代碼有什麼問題,以至於什麼都沒有顯示出來?它甚至不會將用戶恢復到index.html。查詢並顯示mysql搜索結果
我有我的index.html一個搜索欄,如下所示:
<form id="navsearch" class="form-group" role="search" action="search.php" method="post">
<div class="input-group" display="inline">
<input name="search" type="text" value="Search" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button class="btn btn-default btn" name="submit" class="btn btn-lg" type="submit">
<span class="glyphicon glyphicon-search" name="submit"></span>
</button>
</div>
</form>
這裏是 '的search.php' 的PHP:
<?php
//make connection to database
$connection = mysqli_connect('xxx.xxxx', 'root', 'hello', 'awstutorial', 3306);
$search_word = '';
if (isset($_POST['submit'])) {
if (!empty($_POST['search'])) {
$search_word = $_POST['search'];
$search = mysqli_real_escape_string($connection, $_POST['search']);
$query = "SELECT * FROM data WHERE name LIKE '%$search%' ";
$search_query = mysqli_query($connection, $query);
// START RESULTS
if ($result = mysqli_query($connection, $query)) {
$to_display = "<div>";
while ($row = mysqli_fetch_array($result)) {
if ((substr($row['file'], -3) == 'jpg') || (substr($row['file'], -3) == 'JPG') || (substr($row['file'], -3) == 'PNG') || (substr($row['file'], -3) == 'png') || (substr($row['file'], -3) == 'GIF') || (substr($row['file'], -3) == 'gif'))
$img_url = "uploads/" . $row['file'];
else {
if ((strtolower(substr($row['file'], -3) == 'png')) || (strtolower(substr($row['file'], -3) == 'doc')) || (strtolower(substr($row['file'], -3) == 'docx')))
$img_url = "uploads/pdf_logo.png";
else
$img_url = "uploads/no_doc.png";
}
$to_display .= "<a href='story.php?id=".$row['id']."' class='col-md-3 asdivs portfolio-box fancybox fancybox.iframe'><img alt='' class='img-responsive' src='" . $img_url . "'><div class='portfolio-box-caption'>
<div class='portfolio-box-caption-content'>
<div class='project-category text-faded'>
Read
</div>
<div class='project-name'>"
. $row['name'] . "'s Story
</div>
</div>
</div>" . $row['name'] . " - " . $row['email'] . "</a>";
}
}
$to_display .= "</div><div style='clear:both;'></div>";
} else {
header("Location: index.html");
}
}
?>
嘗試在你的PHP安裝[啓動錯誤報告(http://stackoverflow.com/a/21429652/747654),並再次運行該腳本 – wil93
您正在使用類型的按鈕,'submit'這將通過非常自然的方式提交表單 - 它被設置爲'search.php' - 你期望發生什麼?如果你想爲你的用戶提供一個異步的,單一的頁面,沒有重載的經驗,你將需要使用Ajax來運行查詢,而無需重新加載頁面 – RamRaider
刪除'if(isset($ _ POST ['submit'])){...} ' – c4pricorn