【问题标题】:Can't search for a specific record in database无法在数据库中搜索特定记录
【发布时间】: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>

【问题讨论】:

  • &lt;form action="search.php" method="post"&gt; AND isset($_POST['search'] ?
  • phpmyadmin 不是数据库,它只是一个 Web 界面,可让您访问您的数据库及其表。替代方案是 adminer 和独立应用程序 DBeaverMySQL Workbench HeidiSQL 等等......
  • 也许您在查询中的最后一个 % 之后缺少单引号 '?对于测试,我还建议让 php 显示错误以便于调试:stackoverflow.com/questions/5438060/…
  • 反对使用带有错误抑制功能的 MySQL 扩展。

标签: php html search phpmyadmin


【解决方案1】:

忘记任何mysql_ 功能。您正在使用mysql 建立与数据库的连接,但尝试使用mysqli_ 读取结果.. 尝试使用

<?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) {
    $connection = new mysqli("localhost", "root", "","db");
    $filter_Result = $connection->query($query)
    return !$filter_Result ? null : $filter_Result;
}
?>

然后,在你的表单中,替换:

while ($row = $search_result->fetch_array()) {
    ...
    ...
}

提示:

  • 考虑将您的连接建立从filterTable() 函数移动到connection.php 文件并在函数中使用GLOBAL $connection;
  • 将您的连接移动到该文件将允许您随时使用$myVar = $mysqli-&gt;real_escape_string( $myVar ) 转义字符串,这将防止注入

【讨论】:

    【解决方案2】:

    使用mysql_escape_string(); 转义关键字,第二件事不要使用mysql* 使用mysqlipdo,因为mysql* 已从php 7.* 中删除

    $valueToSearch= mysqli_real_escape_string($connect,$_POST['valueToSearch']);
    

    之后使用这个查询

    $query="SELECT * FROM table_name WHERE Student_Name LIKE '%$valueToSearch%'";
    

    因为你有语法错误,如果你回显它你需要这样

    SELECT * FROM table_name WHERE Student_Name LIKE 'something';
    

    我给你举个例子,mysqli 粘贴到connection.php

    $connect=mysqli_connect("localhost","root","","db");
    

    现在你的代码应该是

    include("connection.php");
    if (isset($_POST['search'])) {
        $valueToSearch= mysqli_real_escape_string($connect,$_POST['valueToSearch']);
        $query="SELECT * FROM table_name WHERE Student_Name LIKE '%$valueToSearch%'";
        $search_result=filterTable($query,$connect);
    
    }
    else{
        $query="SELECT * FROM table_name";
        $search_result=filterTable($query,$connect);
    }
    function filterTable($query,$connect)
    {
    
        $filter_Result=mysqli_query($connect,$query);
        if (!$filter_Result) {
            die('query is not valid '.mysqli_error($connect));
        }
        else{
            return $filter_Result;
        }
    
    }
    

    【讨论】:

    • 你为什么要使用函数
    • 运行查询
    • 你需要删除mysql使用mysqli或pdo
    • 我假设连接没有在connection.php中建立,而是在下面的函数中..
    • 你怎么能这样假设
    猜你喜欢
    • 2017-05-14
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多