暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

使用 PHP 和 MySQL 进行高级搜索

原创 黎青峰 2022-10-17
675

本教程帮助使用 PHP 和 Mysql 创建高级搜索功能。我们将创建一个 PHP 表单,它将输入和搜索到 MySQL 表中。

高级搜索为最终用户提供更多选项来过滤搜索结果。

实现高级搜索PHP mysqli的步骤

  • 创建一个 MySQL 数据库并用数据填充它
  • 使用 PHP 创建一个搜索表单。
  • 连接 MySQL 数据库。
  • 将安全性添加到 MySQL 查询中

    mysqli_real_escape_string

    .

创建数据库和 MySQL 表

'test'让我们在 MySQL 服务器中创建一个数据库。在 MySQL 数据库中创建一个员工表。

--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `employees`
--
CREATE TABLE `employees` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `age` int(11) NOT NULL,
  `salary` int(11) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Dumping data for table `employees`
--
INSERT INTO `employees` (`id`, `name`, `age`, `salary`, `created_at`, `updated_at`) VALUES
(1, 'Tiger Nixon', 61, 320800, NULL, NULL),
(2, 'Garrett Winters', 63, 170750, NULL, NULL),
(3, 'Ashton Cox', 66, 86000, NULL, NULL),
(4, 'Cedric Kelly', 22, 433060, NULL, NULL),
(5, 'Airi Satou', 33, 162700, NULL, NULL),
(6, 'Brielle Williamson', 61, 372000, NULL, NULL),
(7, 'Herrod Chandler', 59, 137500, NULL, NULL),
(8, 'Rhona Davidson', 55, 327900, NULL, NULL),
(9, 'Colleen Hurst', 39, 205500, NULL, NULL),
(10, 'Sonya Frost', 23, 103600, NULL, NULL),
(11, 'Jena Gaines', 30, 90560, NULL, NULL),
(12, 'Quinn Flynn', 22, 342000, NULL, NULL),
(13, 'Charde Marshall', 36, 470600, NULL, NULL),
(14, 'Haley Kennedy', 43, 313500, NULL, NULL),
(15, 'Tatyana Fitzpatrick', 19, 385750, NULL, NULL),
(16, 'Michael Silva', 66, 198500, NULL, NULL),
(17, 'Paul Byrd', 64, 725000, NULL, NULL),
(18, 'Gloria Little', 59, 237500, NULL, NULL),
(19, 'Bradley Greer', 41, 132000, NULL, NULL),
(20, 'Dai Rios', 35, 217500, NULL, NULL);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `employees`
--
ALTER TABLE `employees`
  ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `employees`
--
ALTER TABLE `employees`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=58;
COMMIT;

HTML 表单

我们将创建一个 HTML 表单来获取最终用户的输入并提交表单数据。我创建了姓名、年龄和薪水输入字段来获取用户的输入。

<form class="advanced-search-form" method="post" action="index.php">
    <div class="form-group">
    <label for="">Employee Name</label>
    <input type="name" name="search[name]" value="<?php echo $name; ?>">
  </div>
  <div class="form-group">
    <label for="">Age</label>
    <input type="age" name="search[age]" value="<?php echo $age; ?>">
  </div>
  <div class="form-group">
    <label for="">Salary</label>
    <input type="salary" name="search[salary]" value="<?php echo $salary; ?>">
  </div>
  <div class="form-group">
    <input type="submit" value="Search">
  </div>
</form>

提交数据的PHP代码

让我们创建一个 PHP 脚本,它接收来自 HTML 表单输入的代码并使用高级搜索条件形成 MySQL 查询。代码是:

$conn = mysqli_connect("localhost", "root", "", "test");
$queryCondition = " WHERE ";
if(!empty($_POST["search"])) {
foreach($_POST["search"] as $k=>$v){
  if(!empty($v)) {
      $v = mysqli_real_escape_string($v)
    if(!empty($queryCondition)) {
      $queryCondition .= " AND ";
    } else {
      $queryCondition .= " WHERE ";
    }
    
    $queryCondition .= $v ." like '%$v%'";
  }
}
$sql = "SELECT * FROM employees " . $queryCondition;
$result = mysqli_query($conn,$sql);


原文标题:Advanced search with PHP and MySQL

原文作者:Parvez

原文地址:https://www.phpflow.com/php/advanced-search-using-php/

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论