gpt4 book ai didi

php - mysqli php搜索表单空白

转载 作者:行者123 更新时间:2023-11-29 05:18:58 25 4
gpt4 key购买 nike

我正在为我参与的一个小项目学习 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']))

添加error reporting到您的文件的顶部,这将有助于查找错误。

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

// rest of your code

这会发出解析错误的信号。

旁注:错误报告只应在试运行中进行,绝不能在生产中进行。


另外,如评论中所述,最好和更安全的方法是:

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

而不是您目前在表单中使用的内容。


作为旁注:

您应该考虑调查 mysqli with prepared statements , 或 PDO with prepared statements它们更安全

关于php - mysqli php搜索表单空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28677028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com