gpt4 book ai didi

php - 使用 codeigniter 将具有相同输入名称的表单输入插入数据库行

转载 作者:行者123 更新时间:2023-11-29 02:15:31 24 4
gpt4 key购买 nike

我有一个具有类似表单输入名称的表单,我想将其插入到数据库表的不同行中。但是当我提交表单时,不会插入具有相似表单输入名称的输入。

这是我的看法

<div class="row">

<div class="col-xs-12">
<div class="item-block">
<div class="item-form">

<button class="btn btn-danger btn-float btn-remove"><i class="ti-close"></i></button>

<div class="row">
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<input type="text" name="skills_name[]" class="form-control" placeholder="Skill name, e.g. HTML">
</div>
</div>

<div class="col-xs-12 col-sm-6">

<div class="form-group">
<div class="input-group">
<input type="text" name="skills_proficiency[]" class="form-control" placeholder="Skill proficiency, e.g. 90">
<span class="input-group-addon">%</span>
</div>
</div>

</div>
</div>

</div>
</div>
</div>

<div class="col-xs-12 duplicateable-content">
<div class="item-block">
<div class="item-form">

<button class="btn btn-danger btn-float btn-remove"><i class="ti-close"></i></button>

<div class="row">
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<input type="text" name="skills_name[]" class="form-control" placeholder="Skill name, e.g. HTML">
</div>
</div>

<div class="col-xs-12 col-sm-6">

<div class="form-group">
<div class="input-group">
<input type="text" name="skills_proficiency[]" class="form-control" placeholder="Skill proficiency, e.g. 90">
<span class="input-group-addon">%</span>
</div>
</div>

</div>
</div>

</div>
</div>
</div>

<div class="col-xs-12 text-center">
<br>
<button class="btn btn-primary btn-duplicator">Add experience</button>
</div>


</div>

</div>
</section>
<!-- END Skills -->

Controller :

function _get_skills_data(){

$data4['skills_name'] = $this->input->post('skills_name', TRUE);
$data4['skills_proficiency'] = $this->input->post('skills_proficiency', TRUE);
$session_data = $this->session->userdata('logged_in');
$data4['user_id'] = $session_data['user_id'];

return $data4;

}

if ($this->form_validation->run($this) == FALSE)
{
$this->add_resume();
}
else
{

$data4 = $this->_get_skills_data();


$this->User_model->insert_resume('skills',$data4);

$this->session->set_flashdata('message', '<div class="alert alert-success">Resume Added</div>');


redirect('user/manage_resume', 'refresh');
}

型号:

function insert_resume($table, $data){

$query = $this->db->insert($table, $data);
return $this->db->insert_id();

请问我错了什么,我已经阅读了一些对我不起作用的问题。谢谢!

最佳答案

根据 Blinkydamo 的评论,您需要遍历您的帖子数据,因为您尝试将它们作为数组插入

尝试这样的事情:

function _get_skills_data()
{
$arrData = array();
$arrPostSkillsName = $this->input->post('skills_name', TRUE);
$arrPostSkillsProficiency = $this->input->post('skills_proficiency', TRUE);
$session_data = $this->session->userdata('logged_in');

$count = count($arrPostSkillsName);

for($i=0;$i<$count;$i++)
{
$arrItemData = array();
$arrItemData['skills_name'] = $arrPostSkillsName[$i];
$arrItemData['skills_proficiency'] = $arrPostSkillsProficiency[$i];
$arrItemData['user_id'] = $session_data['user_id'];

$arrData[] = $arrItemData;
}

return $arrData;
}

和你的第二个 Controller 函数片段

if ($this->form_validation->run($this) == FALSE)
{
$this->add_resume();
}
else
{
$arrData = $this->_get_skills_data();
foreach($arrData AS $arrItemData)
{
$this->User_model->insert_resume('skills',$arrItemData);
}
$this->session->set_flashdata('message', '<div class="alert alert-success">Resume Added</div>');
redirect('user/manage_resume', 'refresh');
}

关于php - 使用 codeigniter 将具有相同输入名称的表单输入插入数据库行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40464877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com