我有一个带输入框的html页面.我试图基于该输入从数据库中检索数据,并使用php将其发送回原始的html页面.我在外部文件中创建了php,它在浏览器中显示正确的值,但我的问题是尝试在html页面上显示php.我启用我的服务器来...
我有一个带输入框的html页面.我试图基于该输入从数据库中检索数据,并使用php将其发送回原始的html页面.我在外部文件中创建了php,它在浏览器中显示正确的值,但我的问题是尝试在html页面上显示php.
我启用我的服务器来解析html文件并将php放在html文件中,但仍然无法正常工作.当我输入一个值并提交页面重新加载但没有显示任何内容.
如果有人知道这样做的最佳方式,我将不胜感激.我已经阅读了很多线程,但似乎都没有.
PHP:
<?php
define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
$studentnum = $_POST['studentnum'];
$sql = "SELECT * FROM test WHERE number LIKE '%$studentnum%'";
$result=mysqli_query($conn, $sql);
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr>
<th>Name</th>
<th>Number</th>
<th>Floor</th>
<th>Room</th>
<th>Message</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['number'];
echo "</td><td>";
echo $row['floor'];
echo "</td><td>";
echo $row['room'];
echo "</td><td>";
echo $row['message'];
echo "</td></tr>";
}
echo "</table>";
mysqli_close($conn);
?>
HTML:
<form action="test.php" method="post">
<label>Enter Student Number:</label>
<input name="studentnum" type="number" placeholder="Type Here">
<br>
<br>
<input type="submit" value="Enter">
</form>
解决方法:
如果你想在同一页面上全部使用,你可以使用下面的代码,假设你的html文件中的解析php工作正常,你的页面是index.html(在表单操作中指定).
<?php
// check if the form has been submitted and display the results
if (isset($_POST['studentnum'])) {
define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
// escape the post data to prevent injection attacks
$studentnum = mysqli_real_escape_string($conn, $_POST['studentnum']);
$sql = "SELECT * FROM `test` WHERE `number` LIKE '%$studentnum%'";
$result=mysqli_query($conn, $sql);
// check if the query returned a result
if (!$result) {
echo 'There are no results for your search';
} else {
// result to output the table
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr>
<th>Name</th>
<th>Number</th>
<th>Floor</th>
<th>Room</th>
<th>Message</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['number'];
echo "</td><td>";
echo $row['floor'];
echo "</td><td>";
echo $row['room'];
echo "</td><td>";
echo $row['message'];
echo "</td></tr>";
}
echo "</table>";
}
mysqli_close($conn);
} // end submitted
else
{
// not submitted to output the form
?>
<form action="index.html" method="post">
<label>Enter Student Number:</label>
<input name="studentnum" type="number" placeholder="Type Here">
<br>
<br>
<input type="submit" value="Enter">
</form>
<?php
} // end not submitted
?>
沃梦达教程
本文标题为:php – 显示从数据库中提取的数据,基于html表单输入并在html页面中显示
猜你喜欢
- JavaScript中的预解析你了解吗 2023-07-09
- CSS将div内容垂直居中案例总结 2022-11-20
- AJAX实现注册验证用户名 2023-02-23
- Ajax实现无闪烁定时刷新页面实例代码 2022-12-28
- vue-cli4 配置gzip 2023-10-08
- vue中实现图片base64上传和展示 2023-10-08
- vue给页面添加水印,或者给iframe添加水印 2023-10-08
- React Hooks 实现的中文输入组件 2023-07-10
- JavaScript实现放大镜效果 2023-08-08
- electron-vue构建项目 2023-10-08