gpt4 book ai didi

php - Codeigniter:单击提交按钮后没有记录保存在 mysql 数据库中

转载 作者:行者123 更新时间:2023-11-30 22:28:12 25 4
gpt4 key购买 nike

我目前正在研究 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>

这是我的 Controller :(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_payroll', $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

关于php - Codeigniter:单击提交按钮后没有记录保存在 mysql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34644797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com