簡體   English   中英

如何使用php向數據庫表中插入多個值?

[英]How to insert multiple values to database table using php?

請檢查這個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();

        }

請在 while 循環內找到 jQuery Ajax 代碼

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); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM