mysql - 如何在php中的mysql数据库的一个单元格中存储具有单个名称的多个复选框?

标签 mysql codeigniter validation checkbox

` 查询表格

  <div class="row">
      <label class="col-md-3" for="zones">Zones in Bangalore</label>
      <div class="checkboxes">
        <p id="zones"><input type="checkbox" name="zones[]" class="zones">CBD 
          <input type="checkbox" name="zones[]" class="zones">North 
          <input type="checkbox" name="zones[]" class="zones">South  
          <input type="checkbox" name="zones[]" class="zones">East  
          <input type="checkbox" name="zones[]" class="zones">West</p>
      </div>
    </div>

    <div class="row">
        <label class="col-md-3" for="propertytype">Property Type</label>
        <div class="checkboxes">
          <p id="propertytype">
            <input type="checkbox" name="propertytype[]" class="propertytype">Apartment &nbsp;

            <input type="checkbox" name="propertytype[]" class="propertytype">Raw House &nbsp;

            <input type="checkbox" name="propertytype[]" class="propertytype">Villa  &nbsp;
            <input type="checkbox" name="propertytype[]" class="propertytype">Residential Plot</p>
        </div>

      </div>

$('.zones').on('change', function() {
      $('.zones').not(this).prop('checked', false);
       });
      $('.propertytype').on('change', function() {
        $('.propertytype').not(this).prop('checked', false);
      });

这是一段只选择一个复选框的脚本代码。我无法弄清楚如何存储此复选框值。我在尝试存储此数据时遇到错误。我尝试检查 if 条件但无法获取代码。我的项目中有 4 个类似的复选框,我必须将所有数据存储在一个数据库表中。你能帮我解决这个问题吗?

在 Controller 中:

  $numreg= '/^[7-9]{1}[0-9]{9}$/';
  $this->load->helper(array('form', 'url'));
  $this->load->library('form_validation');
 if(isset($_POST['submit'])){
       $this->form_validation->set_rules('zones[]', 'zones of Bangalore',   'required');
       $this->form_validation->set_rules('propertytype[]', 'Property type', 'required');

       }

  if ($this->form_validation->run() == TRUE)
           {
             $data_to_store = array(

               'zones'=> $_POST['zones'],
               'propertytype'=> $_POST['propertytype']

          );

       $this->db->insert('enquiry', $data_to_store);
     }`

最佳答案

您可以序列化复选框值并将其存储在表中。

如下:

$data_to_store = array(

           'zones'=> serialize($_POST['zones']),
           'propertytype'=> serialize($_POST['propertytype'])

      );

您可以通过 unserialize() 反序列化数据,这是一个 PHP 函数。

关于mysql - 如何在php中的mysql数据库的一个单元格中存储具有单个名称的多个复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47886169/

相关文章:

mysql查询帮助(合并行)

php - MYSQL - 用于 SQL 查询的正则表达式

php - 在 Codeigniter 中取消设置 session 数组值

python - Django休息框架: Validate a related field with a list of IDs

javascript - 循环遍历所有表单元素

php - 如何在 codeigniter 中合并所有这些查询代码

java - 如何用JDBC查找已删除记录的主键?

php - 从子文件夹中的 codeigniter 中删除 index.php

php - 使用 SET 减少列值

json - 用于验证要导入 Firebase 的 JSON 的工具