Codeigniter:点击提交按钮后,mysql数据库中没有保存任何记录


Codeigniter: no records has been saved in mysql database after clicking submit button

我目前正在开发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数据库中并没有保存任何记录。我做错了什么?

在您的方法中

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

$query=$this->db->update('tbl_pyroll',$this);

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

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

$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
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');
}
        }