【问题标题】:How to Create a Search Feature with PHP and MySQL? [closed]如何使用 PHP 和 MySQL 创建搜索功能? [关闭]
【发布时间】:2014-06-28 09:09:16
【问题描述】:

下面有一个 HTML 搜索表单。
我需要一些关于我的 PHP 和 MySQL 的帮助。我还是 PHP 和 MySQL 的新手,一位朋友告诉我我需要使用 WHERE 语句,但在我所做的研究中,它向我表明不是这样,或者我理解错了。由于我正忙于一个项目,我非常需要这个:

<form id="advanced_search" action="" class="clearfix" name="advanced_search" method="post">

这是所有标签和文本字段,其中一些是您可以看到的选择字段:

<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="country"><b>Country</b></label>
<input type="text" name="country"><br /><br />
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="location"><b>Location</b></label>
<input type="text" name="location"><br /><br />
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="status"><b>Status</b></label>
<input type="radio" name="status" value="For Rent"><b><span style="color: #0A0000">For Rent</span></b></input><br />
<input type="radio" name="status" value="For Sale"><b><span style="color: #0A0000">For Sale</span></b></input>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="type"><b>Type</b></label>
<input type="text" name="type"><br /><br />
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="bedrooms"><b>Bedrooms</b></label>
<input type="text" name="bedrooms"><br /><br />
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="baths"><b>Bathrooms</b></label>
<input type="text" name="baths"><br /><br />
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="min_price"><b>Min Price</b></label>
<input type="text" name="minprice"><br /><br />
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="max_price"><b>Max Price</b></label>
<input type="text" name="maxprice"><br /><br />
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="condition"><b>Condition of Use</b></label>
<input type="radio" name="condition" value="Furnished"><b><span style="color: #0A0000">Furnished</span></b></input><br />
<input type="radio" name="condition" value="Not Furnished"><b><span style="color: #0A0000">Not Furnished</span></b></input>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="from"><b>From The</b></label>
<input type="radio" name="from" value="Agent"><b><span style="color: #0A0000">Agent</span></b></input><br />
<input type="radio" name="from" value="Private"><b><span style="color: #0A0000">Private</span></b></input>

</div>

这是表单的结尾,但我看到这里没有提交值:

<div class="clearfix"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<a href="#" class="btn btn-inverse btnblock">SEARCHPROPERTY</a>
</div>
</form>

**Here's my PHP I just have done:**

<?php


 if(isset($_POST['search']))
{
$key=$_POST["search"];  //key=pattern to be searched
$con=mysqli_connect("saproperties","localhost"," ","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


**Here by the WHERE and the LIKE, What do I have to put in there?**

$result=mysqli_query($con,"select * from properties where `column_name` like '%$key%'");

while($row=mysqli_fetch_assoc($result))
{

我在这里输入了我的价值观:

$country = $_POST['country'];
$location = $_POST['location'];
$status = $_POST['status'];
$type = $_POST['type'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$minprice = $_POST['min_price'];
$maxprice = $_POST['max_price'];
$conditionofuse = $_POST['conditionofuse'];
$fromthe = $_POST['fromthe'];
}
}

?>

【问题讨论】:

  • 我没有看到任何 PHP 代码
  • 我需要php代码,因为我不明白该怎么做
  • 我还是 PHP 新手
  • 你有数据库吗?你有一个启用了 PHP 的网络服务器吗?您是否看过这个新奇的互联网事物上存在的数百万关于 PHP 和数据库的教程?
  • 看看stackoverflow.com/questions/18542402/…>这是一篇关于如何做到这一点的好文章。

标签: php html mysql


【解决方案1】:

虽然您的问题被否决了一点,但我认为这里有一些空间可以帮助您,也许还有其他可能偶然发现此页面的人。这里实际上需要查看三个组件:HTML、PHP 和数据库。现在,您已经提到您正在使用 MySQL,所以我将围绕它来定制我的答案。

HTML
您在那里编写的 HTML 本质上是一个非常小的表单。注意它使用了post方法,这有点奇怪(你可以搜索一下get和post方法之间的区别),对于这个简单的例子来说应该没问题。不过,您可能需要在该表单中添加一个搜索按钮。我已经包含了一个简单的搜索式表单,我从我使用的登录表单中挪用了它。

<form method="post" action="?p=home" style="text-align:center">
    <input class="input-large" name="searchInput" type="password"><br>
    <button class="btn btn-success" name="searchSubmit" type="submit">Search</button>
</form>

有些类可能不一样(我在一个单独的 CSS 文件中定义了它们,这完全是另一种蠕虫,但你明白了。基本前提是有一个输入字段和一个按钮,并且表单是 post 类型。

PHP
就 PHP 而言,最简单的方法可能是在 HTML 的顶部简单地包含一个 PHP 部分。这是使用标签完成的,标签分别表示 PHP 部分的开始和结束。因此,对于这样的表单,我会在表单所在的同一文件的顶部使用以下内容。

//Checks to see if the user has pressed the search button
if(isset($_POST['searchSubmit'])){
    //When the user presses this button, we run a search query
}

因此,很简单,当用户按下搜索按钮时,我们会使用用户在表单中提供的信息运行 searchFuntion。当然,您可能希望检查用户提供的输入的有效性,并且在网络上的其他地方有很多很好的资源。请注意,$_POST['searchInput'] 将包含我们表单中字段的信息!

MySQL
这是它变得有点棘手的地方,因为您还没有发布任何关于您的数据库的信息,所以我真的不能给您任何具体的建议。话虽如此,我可以告诉你一些关于你的查询应该包含的内容。像这样的简单搜索的大多数查询将包含以下内容:

SELECT * FROM databaseName WHERE searchColumn LIKE '%searchterm%' LIMIT 5

这看起来有点混乱,所以我会分解它。 SELECT * 从名为“databaseName”的数据库中选择所有列,您必须根据自己的数据库结构对其进行更改。 “searchColumn” 术语表示我们正在搜索哪一列(名称、地址、什么?)。 LIKE 'searchterm%' 位指示我们要查找的内容,其中 % 是通配符。 There's a great stack overflow question here about wildcards to use for searching。最后,LIMIT 5 限制了返回的结果数量。当然,自定义查询的方法有很多,您必须多看几眼才能找到适合您的方法。这只是一个例子。

您可以通过 mysqli_query 函数将 MySQL 与 PHP 一起使用,还有很多其他文章关于这样做here 。本文还向您展示了如何使用 mysql_fetch_array 返回和打印结果,如下所示:

mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); //Connect to the the server we are going to use
mysql_select_db("Database_Name") or die(mysql_error()); //Select the database 
$data = mysql_query("YOUR QUERY HERE") ;//Create your query
or die(mysql_error()); //If the query fails, die (I'll explain below)
while($info = mysql_fetch_array( $data )) 
{ 
     //Print out your results here
}

我应该提一下,如果调用 die 命令,它将停止执行您的 PHP,并将在括号中打印消息。在这种情况下,括号中的消息是来自 MySQL 的错误消息。

祝你好运。

【讨论】:

  • 所以其他一切都是正确的,我只需要进行此更改就可以了?
  • 非常感谢您的帮助
  • 您肯定走在正确的轨道上。我注意到当您的表单是 $_POST 类型时,您使用的是 $_GET['country']。如果是这种情况,您的所有变量都应该是 $_POST['varName']。作为事后的想法,您可能希望在尝试让您的查询正常工作之前回显从表单中捕获的变量。
  • 好的,我将对其进行更改,然后您可以告诉我下一步该做什么。那里有哪里和类似的陈述?我应该在那里做什么?
  • 这取决于您的数据库的结构。这里有一个关于 MySQL 关键字搜索的好问题:stackoverflow.com/questions/759580/…
【解决方案2】:

你还没有分享你的php代码!!

只需编写一个 select 语句并使用通配符放置搜索文本,即 % 在搜索变量之前和之后,您可以通过 post 或 get 获取。

【讨论】:

    【解决方案3】:

    以下是用于从数据库表中搜索关键元素的示例 HTML 和 PHP/Mysql 代码

    HTML

    <form method="GET" action="search.php">
    <input type="text" name="search"/>
    <input type="submit" value="search">
     </form>
    

    PHP: search.php

    <?php
    if(isset($_GET['search']))
    {
    $key=$_GET["search"];  //key=pattern to be searched
    $con=mysqli_connect("example.com","peter","abc123","my_db");
    
    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result=mysqli_query($con,"select * from table where `column_name` like '%$key%'"); 
    
    while($row=mysqli_fetch_assoc($result))
    {
    // Print your search variables 
    }
    
    }
    
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多