[英]How to create a search using PHP, mysqli and a html form
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");?>
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.确定您正在使用
mysqli_
进行连接。 If you're using mysql_
or PDO to connect with, that won't work.如果您使用
mysql_
或 PDO 进行连接,那将不起作用。 Those different MySQL APIs do not intermix with each other.这些不同的 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")
do or die(mysqli_error($connection))
来捕捉任何错误,如果有的话。
Add error reporting to 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 mysqli
connection: mysqli
连接示例:
$connection = mysqli_connect("myhost","myuser","mypassw","mybd")
or die("Error " . mysqli_error($connection));
For more information, visit:欲了解更多信息,请访问:
$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 $connection
is the variable declared in your connect.php to connect to your database . $connection
是在 connect.php 中声明的变量,用于连接到数据库。
You should have out the $result
before the $count
.您应该在
$count
之前输出$result
。
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!";}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.