簡體   English   中英

MySQL多個在哪里發布數據PHP

[英]Mysql multiple where with post data php

我正在根據從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);

現在,如果年份,種類,月份和沖突類型文件中的任何一個為空/不為空,如何在查詢末尾添加位置。

如何解決以下問題:

  1. 首先,我們必須找出WHERE子句中是否有任何條件。 只要第一個遇到的條件為真,則if條件為真。

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

注意:您可以將條件設​​置為id(主鍵)在虛擬條件內不為null,因為此條件始終返回true。

  1. 然后,您可以單獨檢查條件。

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

最終查詢將具有WHERE子句,其中至少包含一個條件,即偽條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM