如何在db中插入一个值

时间:2015-06-18 05:58:32

标签: php mysql codeigniter

在我的视图中,我有一个下拉菜单和一个文本框..在我的下拉列表中包含3个值的日薪,每周工资和月薪...以及金额的文本字段..

我的数据库中的

包含每日,每周和每月的3列 如果我在下拉列表中选择一个值,那么在我的文本字段中输入一个金额..该金额将输入特定列,如每日,每周和每月

但显示数据库错误

查看

<label>Mode of Salary</label>
<select name="salary_mode">
     <option disabled="disabled" selected>Select</option>
     <option value="1">Daily </option>
     <option value="2">Weekly</option>
     <option value="3">Monthly</option>
</select></br></br>
<input id="" name="amount_received" type="text" placeholder="Amount Received"  class="form-control">

controlller

for($i=0;$i<count($select_employee);$i++)
{
    $salary_mode=$this->input->post('salary_mode');
    // echo $salary_mode;
    $amount_received=$this->input->post('amount_received');
    // echo $amount_received;
    if($salary_mode==1)
    {
        $daily_salary=$amount_received;
        $weekly_salary=0;
        $monthly_salary=0;
    }
    else if($salary_mode==2)
    {
        $daily_salary=0;
        $weekly_salary=$amount_received;
        $monthly_salary=0;

    }
    else if($salary_mode==3)
    {
        $daily_salary=0;
        $weekly_salary=0;
        $monthly_salary=$amount_received;
    }
    $employee_id =TRIM($select_employee[$i]);
    $issued_date=$this->input->post('issued_date');

    $id=$this->session->userdata('userid');

    $salary_data=array(
        'employee_id'=>$employee_id,
        'user_id'=>$id,
        'daily_salary'=>$daily_salary,
        'weekly_salary'=>$weekly_salary,
        'monthly_salary'=>$monthly_salary,
        'issued_date'=>$issued_date,
    );
    $this->home_model->insert_salary($salary_data);
}

模型

function insert_salary($salary_data)
{
    $this->db->insert('track_salary',$salary_data);
}

1 个答案:

答案 0 :(得分:2)

尝试在for循环之前设置所有三个daily_salary,monthly_salary和weekly_salary = 0,因为你正在使用if else所以当1条件为真时它很明显,其他两个变量将不会被设置。

$daily_salary=0;
$weekly_salary=0;
$monthly_salary=0;

    for($i=0;$i<count($select_employee);$i++)
        {    
            $salary_mode=$this->input->post('salary_mode');
            // echo $salary_mode;
             $amount_received=$this->input->post('amount_received'); 
            // echo $amount_received;
              if($salary_mode==1)
             {
               $daily_salary=$amount_received;  
             }
             else if($salary_mode==2)
             {
                $weekly_salary=$amount_received; 
             }
             else if($salary_mode==3)
             {
                 $monthly_salary=$amount_received;
             }
             $employee_id =TRIM($select_employee[$i]);
             $issued_date=$this->input->post('issued_date');

             $id=$this->session->userdata('userid');

            $salary_data=array(
               'employee_id'=>$employee_id,
               'user_id'=>$id,
               'daily_salary'=>$daily_salary,
               'weekly_salary'=>$weekly_salary,
               'monthly_salary'=>$monthly_salary,
               'issued_date'=>$issued_date,
            );
            $this->home_model->insert_salary($salary_data);
        }
      }