簡體   English   中英

mysqli php搜索表單空白

[英]mysqli php search form blank

我正在學習PHP和MySQL的一個小項目,我參與其中,我正在嘗試使用搜索功能來搜索我的數據庫。 當我嘗試加載我的頁面時,它顯示為空白。 下面是我的search.php頁面的代碼。

<?php
$mysqli_db = new mysqli('localhost', 'root', 'password', 'train');


$result_tb = "";
if (!empty($_POST['SEARCH']) && !empty($_POST['search_value']) {

   $e = $_POST['search_value'];

   $query = 'SELECT * FROM train2015 WHERE ' .
       "name LIKE '%$e%'";

$query_result = $mysqli_db->query($query);

$result_tb = '<table cellspacing="5" cellpadding="5">';
while ($rows = $query_result->fetch_assoc()) {
  foreach ($rows as $k => $v) {
     $result_tb .= '<tr><td>' . $k . '</td><td>' . $v . '</td></tr>';
   }
}
$result_tb .='</table>';

$mysqli_db->close();
}
?>
 <html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Search</title>
</head>
<body>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
     <table>
        <tr>
           <td>
              <input type="text" name="search_value" size="30" maxlength="30"/>
           </td>
           <td>
              <input type="submit" name="SEARCH" value="Search"/>
           </td>
        </tr>
     </table>
  </form>
  <?php echo $result_tb; ?>
  </body>
  </html>

謝謝您幫忙。

你在條件語句的末尾缺少一個括號,它應該是:

if (!empty($_POST['SEARCH']) && !empty($_POST['search_value']))
                                                              ^ right there

但是,最好使用isset()作為提交按鈕,因此:

if(isset($_POST['SEARCH']) && !empty($_POST['search_value']))

錯誤報告添加到文件的頂部,這將有助於查找錯誤。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

哪會發出解析錯誤的信號。

旁注:錯誤報告應該只在暫存中完成,而不是生產。


另外,正如評論中所提到的,使用它是最好和更安全的:

<?php echo htmlspecialchars($_SERVER['PHP_SELF'];) ?>

而不是你目前在表單中使用的內容。


作為旁注:

您應該考慮使用預先准備好的語句來查看mysqli ,或者使用准備好的語句來調查PDO它們會更加安全

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM