Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用MYSQL和PHP与HTML表单进行搜索_Php_Html_Mysql_Mysqli - Fatal编程技术网

使用MYSQL和PHP与HTML表单进行搜索

使用MYSQL和PHP与HTML表单进行搜索,php,html,mysql,mysqli,Php,Html,Mysql,Mysqli,在我的数据库上做一个简单的PHP/SQL搜索栏,结果不会出现。搜索栏出现,我键入的任何内容都不会出现在URL中。代码如下。我正在通过另一个文件连接到数据库 Index.php 首先,将方法类型显式设置为POST: <form action="search.php" method="post"> 如果要使用通配符进行搜索,最好使用LIKE子句 为什么不使用事先准备好的声明: <?php error_reporting(E_ALL); ini_set('display_erro

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

Index.php


首先,将方法类型显式设置为
POST

<form action="search.php" method="post">
如果要使用通配符进行搜索,最好使用
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/>';
        }
    }
}
if(isset($\u POST['search'])){
需要“constants.php”;
$search='%.$\u POST['search'].%';
$query=“选择名称、邮政编码、地址、从邮政编码类似的公园键入?”;
$select=$db\u连接->准备($query);
$select->bind_参数('s',$search);
$select->execute();
$results=$select->get_result();
如果($select->num\u rows>0){
while($row=mysqli\u fetch\u assoc($results)){
//一次循环一行输出
$name=$row[“name”];
$zip=$row[“zip”];
$address=$row[“address”];
$type=$row[“type”];
echo$name.$zip.$address.$type.“
”; } } }
search%的原因是我试图将搜索字段从索引页分配到搜索页,并根据键入的内容进行查询。还是有问题。有几件事已经解决了,但我仍然没有看到任何东西。@JoshuaMobley请确保您的错误报告已打开,我做了一些修改,请查看
<?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/>';
        }
    }
}