Codeigniter:单击提交按钮

时间:2016-01-06 23:26:47

标签: php mysql codeigniter

我目前正在开发codeigniter。我想在数据库中插入记录。但是当我单击“提交”按钮时,数据库中的记录尚未保存。

请帮帮我。谢谢。

这是我的观点(payroll_add.php):

<?php echo form_open('home/saveEmpPayroll',array('class'=>'form-horizontal'));?>

<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Select Employee</label>
    <div class="col-md-9 col-sm-9 col-xs-12">
        <?php echo form_dropdown('empid', $dropdown, '', 'class="form-control" id="empid"'); ?>   
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Basic Salary</label>
    <div class="col-md-9 col-sm-9 col-xs-12">
        <input type="text" id="emp_salary" name="emp_salary" class="form-control">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Total Bus Income for the week</label>
    <div class="col-md-9 col-sm-9 col-xs-12">
        <input type="text" id="total_income" name="total_income" class="form-control">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">SSS Bracket</label>
    <div class="col-md-9 col-sm-9 col-xs-12">
        <select class="form-control" id="emp_SSS">
            <option value="<?php if (isset ($_POST['SSS_bracket'])) {
                echo $_POST ['$SSS_bracket'];}?>"></option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12">SSS Deduction</label>
        <div class="col-md-9 col-sm-9 col-xs-12">
            <input type="text" id="SSSdeduction" name="SSSdeduction" class="form-control" >
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12">Philhealth Bracket</label>
        <div class="col-md-9 col-sm-9 col-xs-12">
            <select class="form-control" id="emp_Philhealth">
                <option value="<?php if (isset ($_POST['Philhealth_bracket'])) {
                    echo $_POST ['$Philhealth_bracket'];}?>"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-12">Philhealth Deduction</label>
            <div class="col-md-9 col-sm-9 col-xs-12">
                <input type="text" id="Philhealthdeduction" name="Philhealthdeduction" class="form-control" >
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-12">Type of Allowance</label>
            <div class="col-md-9 col-sm-9 col-xs-12">
                <input type="text" class="form-control" readonly="readonly" value="Meal Allowance">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-12">Total Allowance</label>
            <div class="col-md-9 col-sm-9 col-xs-12">
                <input type="text" id="emp_allowance" name="emp_allowance" class="form-control" >
            </div>
        </div>
        <div class="ln_solid"></div>
        <div class="form-group">
            <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                <button type="submit" class="btn btn-success" name="emp_submit" id="emp_submit">Submit</button>
            </div>
        </div>
    </form>

这是我的控制器:(home.php)

public function viewAddEmployeePayrollForm() { 
        $this->load->model('Model_payroll');
        $data = array();
        $data['dropdown'] = $this->Model_payroll->get_dropdown();
        $this->load->view('imports/header');
        $this->load->view('imports/menu');
        $this->load->view('payroll/payroll_add', $data);
    }

public function saveEmpPayroll() {
        $this->load->model('Model_payroll');
        $p = new Model_payroll();
        $p->emp_id = $this->input->post('empid');
        $p->basic_salary = $this->input->post('emp_salary');
        $p->meal_allowance = $this->input->post('emp_allowance');
        $p->SSS_bracket = $this->input->post('emp_SSS');
        $p->SSS_deduction = $this->input->post('SSSdeduction');
        $p->Philhealth_bracket = $this->input->post('emp_Philhealth');
        $p->Philhealth_deduction = $this->input->post('Philhealthdeduction');
        $p->bus_income = $this->input->post('total_income');
        $result = $p->saveEmployeePayroll();
        if (!$result) {
            echo mysqli_error($result);
        }
        else {
            redirect('home/goViewEmpPayroll', 'refresh');
        }
    }

这是我的模型(model_payroll.php):

<?php

class Model_payroll extends CI_Model {

    public $emp_id;
    public $basic_salary;
    public $meal_allowance;
    public $SSS_bracket;
    public $SSS_deduction;
    public $Philhealth_bracket;
    public $Philhealth_deduction;
    public $ot_rate;
    public $ot_total;
    public $bus_income;

    public function getEmployeePayroll() {
        $this->db->select("*");
        $this->db->from('tbl_payroll');
        $this->db->join('employees', 'employees.empnum = tbl_payroll.emp_id');
        $query = $this->db->get();
        return $query->result();
    }

    public function addEmployeePayroll() {
        $this->db->where('emp_id', $this->emp_id);
        $query = $this->db->insert('tbl_payroll', $this);
        return $query;
    }

    public function saveEmployeePayroll() {
        if (isset($this->emp_id)) {
            $query = $this->updateEmployeePayroll();
        }
        else {
            $query = $this->addEmployeePayroll();
        }

        return $query;
    }

    public function updateEmployeePayroll() {
        $this->db->where('emp_id', $this->emp_id);
        $query = $this->db->update('tbl_payroll', $this);
        return $query;
    }

    public function get_dropdown() {
        $result = $this->db->select('empnum, name')->get('employees')->result_array();
        $dropdown = array();
        foreach($result as $r) {
            $dropdown[$r['empnum']] = $r['name'];
        }
        return $dropdown;
    }
}

但是点击提交按钮后,没有任何记录保存在mysql数据库中。我做错了什么?

2 个答案:

答案 0 :(得分:0)

在你的方法中

$this->db->update();

Generates an update string and runs the query based on the data you supply. You can pass an array or an object to the function. Here is an example using an array:

$data = array(
               'title' => $title,
               'name' => $name,
               'date' => $date
            );

$this->db->where('id', $id);
$this->db->update('mytable', $data); 

// Produces:
// UPDATE mytable 
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id

$ query = $ this-&gt; db-&gt; update('tbl_payroll',$ this);

$这也包含CI数据。请不要那样使用,请参见下面的例子。

https://ellislab.com/codeigniter/user-guide/database/active_record.html

{{1}}

答案 1 :(得分:0)

You must have to form_validation:

    public function saveEmpPayroll() {
           if($this->form_validation->run()){
            $this->load->model('Model_payroll');
            $p = new Model_payroll();
            $p->emp_id = $this->input->post('empid');
            $p->basic_salary = $this->input->post('emp_salary');
            $p->meal_allowance = $this->input->post('emp_allowance');
            $p->SSS_bracket = $this->input->post('emp_SSS');
            $p->SSS_deduction = $this->input->post('SSSdeduction');
            $p->Philhealth_bracket = $this->input->post('emp_Philhealth');
            $p->Philhealth_deduction = $this->input->post('Philhealthdeduction');
            $p->bus_income = $this->input->post('total_income');
            $result = $p->saveEmployeePayroll();
            if (!$result) {
                echo mysqli_error($result);
            }
            else {
                redirect('home/goViewEmpPayroll', 'refresh');
            }
           }
else{
        redirect('controller/viewAddEmployeePayrollForm');
}
        }