` 查询表格
<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
<input type="checkbox" name="propertytype[]" class="propertytype">Raw House
<input type="checkbox" name="propertytype[]" class="propertytype">Villa
<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/