【发布时间】:2018-03-21 06:37:38
【问题描述】:
我想在数据库中搜索特定记录并将其显示在 html 页面上。我插入了一个带有搜索按钮的搜索栏。我想输入学生姓名并在 html 表中查看该学生的记录。但它不起作用,它在表格中没有显示任何内容。搜索代码如下:
<?php
include("connection.php");
if (isset($_POST['search'])) {
$valueToSearch=$_POST['valueToSearch'];
$query="SELECT * FROM 'table_name' WHERE Student_Name LIKE '%".$valueToSearch."%";
$search_result=filterTable($query);
}
else{
$query="SELECT * FROM 'table_name'";
$search_result=filterTable($query);
}
function filterTable($query)
{
$connect=@mysql_connect("localhost","root","","db");
$filter_Result=@mysql_query($connect,$query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search Record</title>
<style>
table,tr,th,td
{
border:1px solid black;
}
</style>
</head>
<body>
<form action="search.php" method="post">
<input type="text" name="valueToSearch" placeholder="ValueToSearch"><br><br>
<input type="submit" name="search" value="Filter"><br><br>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['age'];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
【问题讨论】:
-
<form action="search.php" method="post">ANDisset($_POST['search']? -
phpmyadmin不是数据库,它只是一个 Web 界面,可让您访问您的数据库及其表。替代方案是adminer和独立应用程序DBeaver、MySQL WorkbenchHeidiSQL等等...... -
也许您在查询中的最后一个
%之后缺少单引号'?对于测试,我还建议让 php 显示错误以便于调试:stackoverflow.com/questions/5438060/… -
反对使用带有错误抑制功能的 MySQL 扩展。
标签: php html search phpmyadmin