【问题标题】:Mysql multiple where with post data phpMysql 多个 where 与 post 数据 php
【发布时间】:2017-04-28 06:03:52
【问题描述】:

我正在根据 php 文件中提供的发布文件过滤数据。 我有 10 个输入字段,用户应该填写任意数量的字段,因为它们不是必填字段。 所以我可能会得到 10 个字段中的 10 个,或者也没有。如何使用 mysql 查询编写 where 条件以仅针对已发布的数据字段过滤来自数据库的数据。

<?php

if (!empty($eventInfo))
    {
    $event = $eventInfo . ',';
    }
  else
    {
    $event = NULL;
    }

if (!empty($wildInfo))
    {
    $wild = $wildInfo . ',';
    }
  else
    {
    $wild = NULL;
    }

if (!empty($additionalInfo))
    {
    $additional = $additionalInfo . ',';
    }
  else
    {
    $additional = NULL;
    }

if (!empty($humanInfo))
    {
    $human = $humanInfo . ',';
    }
  else
    {
    $human = NULL;
    }

if (!empty($livestockInfo))
    {
    $livestock = $livestockInfo . ',';
    }
  else
    {
    $livestock = NULL;
    }

if (!empty($cropInfo))
    {
    $crop = $cropInfo . ',';
    }
  else
    {
    $crop = NULL;
    }

if (!empty($propertyInfo))
    {
    $property = $propertyInfo . ',';
    }
  else
    {
    $property = NULL;
    }

if (!empty($otherInfo))
    {
    $other = $otherInfo . ',';
    }
  else
    {
    $other = NULL;
    }

if (!empty($year))
    {
    $where = "(EXTRACT(year FROM `e`.event_date_time) = '$year')";
    }
  else
    {
    $where = NULL;
    }

if (!empty($month))
    {
    $where1 = "(EXTRACT(month FROM `e`.event_date_time) = '$month')";
    }
  else
    {
    $where1 = NULL;
    }

if (!empty($animal))
    {
    $where2 = "`e`.animal_type = '$animal'";
    }
  else
    {
    $where2 = NULL;
    }

if (!empty($conflictType))
    {
    $like = '`e`.event_types like %' . $conflictType . '%';
    }

$query = "SELECT $event" . "$wild" . "$additional" . "$human" . "$livestock" . "$crop" . "$property" . "$other" . "`d`.`value` as `event_district`, `vdc`.`value` as `event_vdc_munic`   FROM `event_info` as `e` LEFT JOIN `wild_animal_info` `wa` ON `e`.`event_id` = `wa`.`event_id` LEFT JOIN `human_victim_info` `hv` ON `e`.`event_id` = `hv`.`event_id` LEFT JOIN `livestock_destruction_info` `lv` ON `e`.`event_id` = `lv`.`event_id` LEFT JOIN `crop_destruction_info` `c` ON `e`.`event_id` = `c`.`event_id` LEFT JOIN `proprty_destruction_info` `p` ON `e`.`event_id` = `p`.`event_id` LEFT JOIN `other_destruction_info` `o` ON `e`.`event_id` = `o`.`event_id` LEFT JOIN `additional_info` `a` ON `e`.`event_id` = `a`.`event_id` LEFT JOIN `district_info` `d` ON `e`.`event_district` = `d`.`id` LEFT JOIN `vdc_munic_info` `vdc` ON `e`.`event_vdc_munic` = `vdc`.`id`";
$select_table = pdo_db()->query($query);
$rows = $select_table->fetch(PDO::FETCH_ASSOC);

现在,如果年份、物种、月份和冲突类型字段中的任何一个为空/非空,我如何在查询末尾添加位置。

【问题讨论】:

    标签: php mysql mysqli pdo


    【解决方案1】:

    下面的解决方案怎么样:

    1. 首先我们要找出 WHERE 子句中是否有条件。只要第一个遇到的条件中的任何一个为真,if 条件就为真。

      if($where1 || $where2 || $where3 || ...){ $query = ' ... WHERE dummy-condition'; }

    注意:您可以将条件设​​置为 id(Primary Key) 在虚拟条件中不为空,因为此条件始终返回 true。

    1. 然后你可以单独检查条件。

      if($where1) $query = $query + ' AND condition($where1)'; if($where2) $query = $query + ' AND condition($where2)';

    最终查询将包含至少一个条件的 WHERE 子句,即虚拟条件。

    【讨论】:

    • 感恩先生!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2019-02-03
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多