Search code examples
codeignitercodeigniter-2

Unknown column in where clause while parsing csv to update table


I am parsing a CSV file to add records to a table. this is how I do it.

public function parse_csv($file) {

        $this->load->library('CSVReader');
        $csvData = $this->csvreader->parse_file($file);


        foreach($csvData as $key => $row) {
            $data_n[$key] = array(
                'mf_date' => $row['mf_date'],
                'mf_student_id' => $row['mf_student_id'],
                'mf_sender' => $row['mf_sender'],
                'mf_amount' => $row['mf_amount'],
                'mf_trx_id' => $row['mf_trx_id'],
                );
            $this->db->insert('monthly_fee', $data_n[$key]);
            $this->student_model->update_monthly_due($row['mf_student_id'], $row['mf_amount']);
        }
    }

I wish to update another table where there is three columns

1)md_student_id //unique
2)md_due
3)md_paid

I want to update that table while uploading the csv where mf_student_id and md_student_id matches with the model function below.

public function update_monthly_due($mf_student_id, $mf_amount)
    {   
        $this->db->select('mf_student_id');
        $this->db->from('monthly_fee');
        $this->db->join('monthly_due', 'monthly_due.md_student_id = monthly_fee.mf_student_id');
        $sql = "UPDATE monthly_due set md_due = md_due - " . $mf_amount . ", md_paid = md_paid + " . $mf_amount . " WHERE md_student_id = " . $mf_student_id;
        $this->db->query($sql);
    }

But when I upload a csv it generates the following error.

Error Number: 1054

Unknown column 'MCS20145B41' in 'where clause'

UPDATE monthly_due set md_due = md_due - 5500, md_paid = md_paid + 5500 WHERE md_student_id = MCS20145B41

Filename: F:\xampp\htdocs\student\system\database\DB_driver.php

Line Number: 330

What I am doing wrong?!

Thanks in advance


Solution

  • you should replace this line as follow

    $sql = "UPDATE monthly_due set md_due = md_due - " . $mf_amount . ", md_paid = md_paid + " . $mf_amount . " WHERE md_student_id = '" . $mf_student_id . "'";
    

    take attention to the end of this line