无法将多选数据存储到数据库
unable to store the multiselect data to database
我正在尝试将值存储在数据库中,所有值都在数据库中被破坏,但问题出在下拉列表上。当我尝试仅上传多选下拉菜单时
最后选择的值正在存储,因为我想将所有选择的值存储在数据库中,有人可以指导我如何做到这一点
//----controller-----
public function portfolio1()
{
$this->form_validation->set_rules('first_content', 'First content', 'required');
$this->form_validation->set_rules('second_content', 'Second content', 'required');
$this->form_validation->set_rules('type', 'Type', 'required');
if ($this->form_validation->run()==TRUE)
{
$config['upload_path'] = FCPATH.'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( $this->upload->do_upload('filename') )
{
//print_r($this->upload->data());die;
$data['first_content'] = $this->input->post('first_content');
$data['second_content'] = $this->input->post('second_content');
$data['type'] = $this->input->post('type');
$data['filename'] = $this->upload->data('file_name');
//Transfering data to Model
$this->Contact_model->portfolio1($data);
//Redirecting to success page
redirect(site_url('Home/portfolio1'));
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
}
}
else
{
$this->load->view('portfolio1');
}
}
//----model----------
function portfolio1($data)
{
//saving records
$this->db->insert('portfolio1', $data);
}
//------view page-----
<?php echo form_open_multipart('Home/portfolio1'); ?>
<div style="padding-top:10%; padding-left:30%">
<div>
<textarea name="first_content" rows="4" cols="50"></textarea>
<span><?php echo form_error("first_content");?></span>
</div><br>
<div>
<textarea name="second_content" rows="4" cols="50"></textarea>
<span><?php echo form_error("second_content");?></span>
</div><br>
<div>
<select name="type" multiple>
<option value="*">All Works</option>
<option value=".bootstrap">Creative</option>
<option value=".html">Photography</option>
<option value=".wordpress">Web Development</option>
</select>
<span><?php echo form_error("type");?></span>
</div><br>
<div>
<input type="file" name="filename">
<span><?php echo form_error("filename");?></span>
</div><br>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
all the values are getting dtored in the database but the issue is with dropdown. when i try to upload the multiselect dropdown only the last selected value is getting stored
你能说一下你的多选是用哪个名字发布的吗,我找不到你的视图页面来检查你的属性在下拉列表中是如何提到的
像这样更改您的多选部分。
<select name="type[]" multiple>
<option value="*">All Works</option>
<option value=".bootstrap">Creative</option>
<option value=".html">Photography</option>
<option value=".wordpress">Web Development</option>
</select>
我已将名称从 type
更改为 type[]
,因此您将获得所有选定的选项。
更新
if ( $this->upload->do_upload('filename') )
{
//print_r($this->upload->data());die;
$data['first_content'] = $this->input->post('first_content');
$data['second_content'] = $this->input->post('second_content');
$data['type'] = implode(",",$this->input->post('type'));
$data['filename'] = $this->upload->data('file_name');
//Transfering data to Model
$this->Contact_model->portfolio1($data);
//Redirecting to success page
redirect(site_url('Home/portfolio1'));
}
现在它将 type
保存为 table 中的逗号分隔。
如果你的下拉框是 multiselect 那么你必须在 select 属性的名称中使用数组。
<select name="type[]" multiple>
您将获得 post 变量中的所有 selected 值,如
<?Php $_POST["type"]; ?>
我以前也遇到过这个问题,但我在服务器端有解决办法。
通常你会得到你的数组值,在你的例子中,在 $_POST['type'] 中,对吧?
所以你想做的就是这样做......
$type = implode(',', $_POST['type']);
并将其作为逗号分隔值保存到您的数据库中。
每次在表单中显示这些字段时,您都会将它们转换回数组形式并遍历每个字段,以便默认选中它们。
$type = explode(',', $_POST['type']);
为了统一起见,我将只使用 $_POST 变量,但我想您已经明白了。希望这对您有所帮助!
我正在尝试将值存储在数据库中,所有值都在数据库中被破坏,但问题出在下拉列表上。当我尝试仅上传多选下拉菜单时 最后选择的值正在存储,因为我想将所有选择的值存储在数据库中,有人可以指导我如何做到这一点
//----controller-----
public function portfolio1()
{
$this->form_validation->set_rules('first_content', 'First content', 'required');
$this->form_validation->set_rules('second_content', 'Second content', 'required');
$this->form_validation->set_rules('type', 'Type', 'required');
if ($this->form_validation->run()==TRUE)
{
$config['upload_path'] = FCPATH.'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( $this->upload->do_upload('filename') )
{
//print_r($this->upload->data());die;
$data['first_content'] = $this->input->post('first_content');
$data['second_content'] = $this->input->post('second_content');
$data['type'] = $this->input->post('type');
$data['filename'] = $this->upload->data('file_name');
//Transfering data to Model
$this->Contact_model->portfolio1($data);
//Redirecting to success page
redirect(site_url('Home/portfolio1'));
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
}
}
else
{
$this->load->view('portfolio1');
}
}
//----model----------
function portfolio1($data)
{
//saving records
$this->db->insert('portfolio1', $data);
}
//------view page-----
<?php echo form_open_multipart('Home/portfolio1'); ?>
<div style="padding-top:10%; padding-left:30%">
<div>
<textarea name="first_content" rows="4" cols="50"></textarea>
<span><?php echo form_error("first_content");?></span>
</div><br>
<div>
<textarea name="second_content" rows="4" cols="50"></textarea>
<span><?php echo form_error("second_content");?></span>
</div><br>
<div>
<select name="type" multiple>
<option value="*">All Works</option>
<option value=".bootstrap">Creative</option>
<option value=".html">Photography</option>
<option value=".wordpress">Web Development</option>
</select>
<span><?php echo form_error("type");?></span>
</div><br>
<div>
<input type="file" name="filename">
<span><?php echo form_error("filename");?></span>
</div><br>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
all the values are getting dtored in the database but the issue is with dropdown. when i try to upload the multiselect dropdown only the last selected value is getting stored
你能说一下你的多选是用哪个名字发布的吗,我找不到你的视图页面来检查你的属性在下拉列表中是如何提到的
像这样更改您的多选部分。
<select name="type[]" multiple>
<option value="*">All Works</option>
<option value=".bootstrap">Creative</option>
<option value=".html">Photography</option>
<option value=".wordpress">Web Development</option>
</select>
我已将名称从 type
更改为 type[]
,因此您将获得所有选定的选项。
更新
if ( $this->upload->do_upload('filename') )
{
//print_r($this->upload->data());die;
$data['first_content'] = $this->input->post('first_content');
$data['second_content'] = $this->input->post('second_content');
$data['type'] = implode(",",$this->input->post('type'));
$data['filename'] = $this->upload->data('file_name');
//Transfering data to Model
$this->Contact_model->portfolio1($data);
//Redirecting to success page
redirect(site_url('Home/portfolio1'));
}
现在它将 type
保存为 table 中的逗号分隔。
如果你的下拉框是 multiselect 那么你必须在 select 属性的名称中使用数组。
<select name="type[]" multiple>
您将获得 post 变量中的所有 selected 值,如
<?Php $_POST["type"]; ?>
我以前也遇到过这个问题,但我在服务器端有解决办法。
通常你会得到你的数组值,在你的例子中,在 $_POST['type'] 中,对吧? 所以你想做的就是这样做......
$type = implode(',', $_POST['type']);
并将其作为逗号分隔值保存到您的数据库中。 每次在表单中显示这些字段时,您都会将它们转换回数组形式并遍历每个字段,以便默认选中它们。
$type = explode(',', $_POST['type']);
为了统一起见,我将只使用 $_POST 变量,但我想您已经明白了。希望这对您有所帮助!