如何使用 PHP、mysqli 和 html 表单创建搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29347256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:22:03  来源:igfitidea点击:

How to create a search using PHP, mysqli and a html form

phpsearch

提问by Rebekah

I want to create a form which allows the user to type in a search and have it pick up the right values from a database and display them, for some reason I can't get my query to work it just displays "could not search"

我想创建一个表单,它允许用户输入搜索并让它从数据库中选择正确的值并显示它们,由于某种原因我无法让我的查询工作它只显示“无法搜索”

Here is my php code

这是我的 php 代码

 <?php

  include "connect.php";

  $output = '';

  if(isset($_POST['search'])) {
    $search = $_POST['search'];
    $search = preg_replace("#[^0-9a-z]i#","", $search);

    $query = mysqli_query("SELECT * FROM house WHERE town LIKE '%$search%'") or die ("Could not search");
    $count = mysqli_num_rows($query);
    
    if($count == 0){
      $output = "There was no search results!";

    }else{

      while ($row = mysqli_fetch_array($query)) {

        $town = $row ['town'];
        $street = $row ['street'];
        $bedrooms = $row ['bedrooms'];
        $bathroom = $row ['bathrooms'];

        $output .='<div> '.$town.''.$street.''.$bedrooms.''.$bathrooms.'</div>';

      }

    }
  }

  ?>

Here is my form

这是我的表格

 <form action ="home.php" method = "post">
  
          <input name="search" type="text" size="30" placeholder="Belfast"/>

          <input type="submit" value="Search"/>

          </form> 

          <?php print ("$output");?>

采纳答案by Funk Forty Niner

You're not connecting to your DB in your query:

您没有在查询中连接到数据库:

$query = mysqli_query("SELECT
                      ^ missing connection variable

there is no connection variable (unknown what you are using to connect with)

没有连接变量(不知道你用什么来连接)

$query = mysqli_query($connection, "SELECT ...
                      ^^^^^^^^^^^^

From the manual http://php.net/manual/en/mysqli.query.php

从手册http://php.net/manual/en/mysqli.query.php

Object oriented style mixed mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

面向对象的风格 mixed mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

Procedural style mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

程序风格 mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

figuring you are using mysqli_to connect with. If you're using mysql_or PDO to connect with, that won't work. Those different MySQL APIs do not intermix with each other.

确定您正在使用mysqli_连接。如果您正在使用mysql_或 PDO 进行连接,那将不起作用。这些不同的 MySQL API 不会相互混合。

Plus, instead of or die ("Could not search")do or die(mysqli_error($connection))to catch any errors, if any.

此外,而不是or die ("Could not search")or die(mysqli_error($connection))赶上任何错误,如果有的话。

Add error reportingto the top of your file(s) which will help find errors.

错误报告添加到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote:Error reporting should only be done in staging, and never production.

旁注:错误报告应该只在登台时进行,而不要在生产中进行。



Example mysqliconnection:

示例mysqli连接:

$connection = mysqli_connect("myhost","myuser","mypassw","mybd") 
                or die("Error " . mysqli_error($connection)); 

For more information, visit:

欲了解更多信息,请访问:

回答by reyven

$query = mysqli_query("SELECT * FROM house WHERE town LIKE '%$search%'") or die ("Could not search");

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

$count = mysqli_num_rows($result);

The $connectionis the variable declared in your connect.php to connect to your database .

$connection是你connect.php声明的变量,以连接到数据库。

You should have out the $resultbefore the $count.

你应该$result$count.

回答by butter bee

Try to remove the space between if($count==0)it will start working!!

尝试删除if($count==0)它之间的空间将开始工作!

if($count==0){
      $output = "There was no search results!";}