【问题标题】:Search using MYSQL and PHP with HTML form使用带有 HTML 表单的 MYSQL 和 PHP 进行搜索
【发布时间】:2016-02-17 10:22:45
【问题描述】:

在我的数据库上做一个简单的 PHP/SQL 搜索栏,结果没有出现。搜索栏出现,我输入的任何内容都没有出现在 URL 中。代码如下。我正在通过不同的文件连接到数据库。

Index.php

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <center>    		
        <form action="search.php" method="post">
		<input type="text" name="search" autocomplete="off">
        	<input type="submit" value="search">
        </form>
    </center>
</body>
</html>

search.php

<?php
	$search = $_GET['search'];
	require 'constants.php';
?>

<?php

$query = "SELECT Name, Zip, Address, Type FROM parks WHERE Zip = '%{$search}%'";

$result = mysqli_query($db_connection,$query);

while ($row = mysqli_fetch_array($result))
{
    // loop through output one row at a time
    $name = 		$row["Name"];
    $zip = 		$row["Zip"];
    $address = 		$row["Address";
    $type = 	$row["Type"];

    echo $name . $zip . $address . $type;
}

?>

【问题讨论】:

    标签: php html mysql mysqli


    【解决方案1】:

    首先,您将方法类型明确设置为POST

    <form action="search.php" method="post">
    

    那么,您正在尝试获取以下值:

    <input type="text" name="search" autocomplete="off">
    

    通过$search = $_GET['search'];。使用$_POST['search'];

    第二,这没有意义

    WHERE Zip = '%{$search}%'";
    

    如果要使用通配符进行搜索,最好使用LIKE 子句。

    为什么不使用准备好的语句:

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    if(isset($_POST['search'])) {
        require 'constants.php';
        $search = '%' . $_POST['search'] . '%';
        $query = "SELECT Name, Zip, Address, Type FROM parks WHERE Zip LIKE ?";
    
        $select = $db_connection->prepare($query);
        $select->bind_param('s', $search);
        $select->execute();
        $select->store_result();
    
        if($select->num_rows > 0) {
            $select->bind_result($name, $zip, $address, $type);
            while($select->fetch()) {
                // loop through output one row at a time
    
                echo $name . $zip . $address . $type . '<br/>';
            }
        }
    
    }
    ?>
    

    另一种获取方式:

    if(isset($_POST['search'])) {
        require 'constants.php';
        $search = '%' . $_POST['search'] . '%';
        $query = "SELECT Name, Zip, Address, Type FROM parks WHERE Zip LIKE ?";
    
        $select = $db_connection->prepare($query);
        $select->bind_param('s', $search);
        $select->execute();
        $results = $select->get_result();
    
        if($select->num_rows > 0) {
            while($row = mysqli_fetch_assoc($results)) {
                // loop through output one row at a time
                $name = $row["Name"];
                $zip = $row["Zip"];
                $address = $row["Address"];
                $type = $row["Type"];
    
                echo $name . $zip . $address . $type . '<br/>';
            }
        }
    }
    

    【讨论】:

    • "%search%" 认为是因为我试图将搜索字段从索引页面分配到搜索页面,并根据输入的内容进行查询。仍然有这个问题。一些问题已得到修复,但我仍然没有出现任何内容。
    • @JoshuaMobley 确保您的错误报告已打开,我做了一些修改,看看
    猜你喜欢
    • 1970-01-01
    • 2016-05-01
    • 2022-01-04
    • 1970-01-01
    • 2011-03-21
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多