如何使用php向数据库表中插入多个值?

时间:2019-08-23 23:45:45

标签: javascript php jquery sql-server codeigniter

请检查此jsfiddle。我的结果是这样的,

http://jsfiddle.net/kz1vfnx2/

我需要使用PHP Codeigniter将这些数据每行一张一张地存储到数据库(sql server)。插入表格的样子

   Date           Frequency
05-Feb-2019   1st Basic Treatment
12-Mar-2019   2nd Control Treatment
----------------------------------
--------------------------------

单击按钮时调用该函数并插入数据库

    $('#saveactivityarea').on('click', function(event) { //save new activity area
    var act_contractbranch_firstjobdt = "2019-01-01";
    var Contractend_firstjobdt = "2020-01-01";
    var act_job_freq_daysbtw= "30";
    saveschedule(act_contractbranch_firstjobdt,Contractend_firstjobdt,act_job_freq_daysbtw,0);

var contractID = $('#contractID').val();
var act_job_freq_contract = $("#act_job_freq_contract option:selected").val();
$.ajax({
          type: "POST",
          url: 'activity_submitted',
          data: {
            //here i need to pass date and frequency. insert to table like one by one row
            getcontract_id: contractID,
            getcontractbranch_firstjobdt: act_contractbranch_firstjobdt,
            //etc....

          }, 
          success: function(data) {
          alert('success')
}

    })

PHP模态功能

$data_jobschedule = array(
            'Contract_id' => $this->input->post('getcontract_id'),
            'job_freq_id' => $this->input->post('getcontractbranch_freq')
        );
$insert_id = 0;
        if ($this->db->insert("job_schedule", $data_jobschedule))
            $insert_id = $this->db->insert_id();

        }

1 个答案:

答案 0 :(得分:0)

请在此处找到jQuery Ajax代码 在while循环内

var dataArray = [];
while(condition) {
   details = [];
   //do your calculations
   details['date'] = date;
   details['frequency'] = frequency;
   dataArray[] = details;
}

$.ajax({
    url: "<?php echo site_url('activity_submitted'); ?>",
    data: {dateArray: dataArray},
    success: function(data){
        alert('success');
    },
    error: function() { alert("Error."); }


    });

在控制器和模型中,您需要获取数据并将其插入表中。

$data = $_REQUEST['dateArray'];
$this->db->insert_batch('mytable', $data);