修改我的php-mysql机制,在结果中包括一个自定义的多个复选框值


Modifying my php-mysql mechanism to include in the results a custom mutiple checkbox values

我有以下PHP MySql查询形成机制,以便处理用户放入表单中的数据,并从我的数据库中获取相关结果。

<?php
// build array of field names=============================================================================
$fields=array('user','customer','vessel','country',
             'port','eta','service_station',
             'case_reference','status');
// initialize empty array for WHERE clauses
$wheres=array();
// loop through field names, get POSTed values,
// and build array of WHERE clauses, excluding false values
foreach ($fields as $field) {
  // get existing field value from POST, mark missing or empty value as FALSE
  ${$field} = isset($_POST[$field]) && trim($_POST[$field])!=''
      ? trim($_POST[$field]) : false;
  // add to array of WHERE clauses only if value is not FALSE
  if (${$field}) { $wheres[]="$field LIKE '".${$field}."%'"; }
}
    $sql="SELECT * FROM jobs WHERE ".
         (!empty($wheres) ? implode(" AND ",$wheres) : '1=1').
         ";";
?>

<html>
<head>
  <title></title>
</head>
<body>
  <label name="user"><input/></label>
  <label name="customer"><input/></label>
  <label name="vessel"><input/></label>
  <label name="country"><input/></label>
  <label name="port"><input/></label>
  <label name="eta"><input/></label>
  <label name="service_station"><input/></label>
  <label name="case_reference"><input/></label>
  <label name="status"><input/></label>
</body>

我想在数据库中添加一个名为type_of_service的列。但在我的搜索表单中,我想根据服务类型添加一个复选框列表,这样用户就可以同时输入多个类型:

<div id="multiselect" class="field">
    <div>
      <label><input type="checkbox" name="type_of_service[]" value="Fire Extinguishers">Fire Extinguishers</label></br>
      <label><input type="checkbox" name="type_of_service[]" value="Fixed CO2 Systems">Fixed CO2 Systems</label></br>
      <label><input type="checkbox" name="type_of_service[]" value="Fixed Foam Systems">Fixed Foam Systems</label>
    </div>
    <div>
      <label><input type="checkbox" name="type_of_service[]" value="Liferafts">Liferafts</label></br>
      <label><input type="checkbox" name="type_of_service[]" value="Lifeboats & Davits">Lifeboats & Davits</label></br>
      <label><input type="checkbox" name="type_of_service[]" value="Immersion Suits & Lifejackets">Immersion Suits & Lifejackets</label>
    </div>
    <div>
      <label><input type="checkbox" name="type_of_service[]" value="Breathing Apparatus">Breathing Apparatus</label></br>
      <label><input type="checkbox" name="type_of_service[]" value="Medical Oxygen Resuscitators">Medical Oxygen Resuscitators</label></br>
      <label><input type="checkbox" name="type_of_service[]" value="Emergency Escape Sets">Emergency Escape Sets</label></br>
    </div>
    <div>
      <label><input type="checkbox" name="type_of_service[]" value="New Supply">New Supply</label></br>
      <label><input type="checkbox" name="type_of_service[]" value="Other">Other Equipment/Works</label>
    </div>        
  </div>

我应该以什么方式修改我的PHP MySQL查询形成机制,以包括这一点并成为ab;le也可以搜索复选框???

您有一个设计问题。你的数据是如何结构化的。

你有几个选择:

  • 你用逗号左右来连接你的类型(非常难看)
  • 你把每个选项都设为一列(非常难看)
  • 您制作了一个单独的表来存储与job_id绑定的类型。检索类型时,可以按select type from job_types where job_id = 1搜索该表(最佳选项)

通过复选框和一行循环到具有相应job_idjob_types表。然后你的数据就有了良好的结构。