php - 可以从多个表单将多个数据插入到单个表中吗?

标签 php mysql codeigniter

我正在使用 codeigniter。我有多个表单,但只有在我到达最后一个表单后,我才需要一个提交操作,该操作应该插入所有表单中的所有数据。这个怎么做? 我有一个 Controller 文件。

<?php
class Admin_service extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('service_model');

        if(!$this->session->userdata('is_logged_in')){
            redirect('admin/login');
        }
    }


    public function add1()
    {

        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
$this->load->library('session');
            $this->session->unset_userdata('service_detail');

            $this->form_validation->set_rules('backbar', 'backbar');


            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
                $data_to_store = array(
                'id' => $this->input->post('service_detail'),
                    'service_name' => $this->input->post('backbar')

                );
                if($this->service_model->store_service($data_to_store)){
                    $data['flash_message'] = TRUE; 
                    $this->session->set_flashdata('flash_message', 'updated');

                }else{
                    $data['flash_message'] = FALSE; 
                }

            }

        }
        $data['category'] = $this->category_model->get_category();
        $data['main_content'] = 'admin/service/add1';
        $this->load->view('includes/template', $data);  

    }

    public function add()
    {
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
    $this->load->library('session');
            $this->session->unset_userdata('service_detail');
            $this->form_validation->set_rules('id', 'id');
            $this->form_validation->set_rules('service_name', 'service_name','alpha');
            $this->form_validation->set_rules('category', 'category');
            $this->form_validation->set_rules('service_tax', 'service_tax');
            $this->form_validation->set_rules('service_length', 'service_length','numeric');
            $this->form_validation->set_rules('service_price', 'service_price','numeric');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            if ($this->form_validation->run())
            {
                $data_to_store = array(
                'id' => $this->input->post('id'),
                    'service_name' => $this->input->post('service_name'),
                    'category' => $this->input->post('category'),
                    'service_tax' => $this->input->post('service_tax'),
                    'service_length' => $this->input->post('service_length'),          
                    'service_price' => $this->input->post('service_price')
                );

                if($this->service_model->store_service($data_to_store)){
                    $data['flash_message'] = TRUE; 
                    $this->session->set_flashdata('flash_message', 'updated');
                    $this->session->set_userdata('service_detail', ['service_price'=>$this->input->post('service_price'),"service_tax"=>$this->input->post('service_tax')]);
                    redirect(base_url().'admin/service/view');
                }else{
                    $data['flash_message'] = FALSE; 
                }

            }

        }

        $data['main_content'] = 'admin/service/add';
        $this->load->view('includes/template', $data);  
    }       



}

这里我有两个函数 addadd1,每个函数加载一个单独的 View ,我有我的表单。在 add 表单之后,我需要从 add1 表单中获取数据,并在我单击第二个表单中的提交按钮时插入到单个表中。怎么做?有人可以帮我编码吗?

最佳答案

在用户提交表单的 html 页面 上添加一个复选框:

<input type="checkbox" name="is_finish" value="1" />

在您的 Controller 中修改 if ($this->form_validation->run()) 条件中的代码如下:

if ($this->form_validation->run())
{

    $this->load->library('session');
    $service_details[] = array(
        'id' => $this->input->post('id'),
        'service_name' => $this->input->post('service_name'),
        'category' => $this->input->post('category'),
        'service_tax' => $this->input->post('service_tax'),
        'service_length' => $this->input->post('service_length'),          
        'service_price' => $this->input->post('service_price')
        );

    $service_details_sess = $this->session->userdata('service_details');

    if($service_details_sess != "") {
        $service_details[] = $service_details_sess;
    }

    $this->session->set_userdata('service_details',$service_details);

    $finish = $this->input->post('is_finish');

    if($finish == "1")
    {
        $this->service_model->store_service($service_details);
    }
}

修改service_model模型的store_service函数如下:

function store_service($service_details) {
    $this->load->library('session');
    $this->session->unset_userdata('service_details');
    $this->db->insert_batch($service_details);
    if ($this->db->affected_rows() > 0)
    {
        return TRUE;
    }
        return FALSE;
}

HTML 部分:

<button id="store_data" type="button">Store</button>
<script type="text/javascript">
$("#store_data").click(function(event) {
    $.ajax({
        url: 'path to your controller that store data',
        type: 'POST',
        data: $("#your_form_id").serialize(),
    })
    .done(function() {
        console.log("success");
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });

});
</script>

关于php - 可以从多个表单将多个数据插入到单个表中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31717237/

相关文章:

mysql - Rails - mysql 临时表在不同的 http 请求之间是否可用

php - 调用非对象上的成员函数 userdata()

php - codeigniter 在另一个查询中使用部分查询结果

PHP MySQL 表继承

php - mysql_num_rows()参数无效

PHP MySQL 语法错误

mysql left join问题

mysql - 如何为每个用户仅选择最新行?

php - CodeIgniter 大批量插入

javascript - 如何仅删除以 "//"开头的 JavaScript 注释?