1
votes

Not inserting..Any Suggestions?? DB Driver: mysqli using Codeigniter.

Controller

function add_quote()
    {

        $this->form_validation->set_rules('invoice_no', $this->lang->line("invoice_no"));
        $this->form_validation->set_rules('date', $this->lang->line("date"), 'required');
        $this->form_validation->set_rules('customer', $this->lang->line("customer"), 'required');
        if($this->input->post('customer') == 'new') {
            $this->form_validation->set_rules('state', $this->lang->line("state"));
            $this->form_validation->set_rules('gstin', $this->lang->line("gstin"));
            $this->form_validation->set_rules('company', $this->lang->line("company")." ".$this->lang->line("name"), 'required');
            $this->form_validation->set_rules('email', $this->lang->line("customer")." ".$this->lang->line("email_address"), 'required|valid_email|is_unique[customers.email]');
            $this->form_validation->set_rules('phone', $this->lang->line("phone"), 'required|min_length[6]|max_length[16]');
        }

        if ($this->form_validation->run() == true) {

            print_r("helo World");
            exit;

            $form = $this->sales_model->process_form();
            $customer_data = $form['customer_data'];
            $products = $form['products'];
            $data = $form['data'];
            $dum = 'Q-'.$data['reference_no'];
            $data['reference_no'] = $dum;
            //unset($data['due_date'], $data['recurring']);

             //echo '<pre />'; var_dump($data); var_dump($products); die();
        }

        //$data1 = array('reference_no' => 1);
        //$this->db->insert('customers',$data1);
        if ($this->form_validation->run() == true && $this->sales_model->addQuote($data, $products, $customer_data)) {

            $this->session->set_flashdata('message', $this->lang->line("quote_added"));
            redirect("sales/quotes");

        } else {

            $this->data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['inv'] = false;
            $this->data['q'] = true;
            $this->data['customers'] = $this->sales_model->getAllCustomers();
            $this->data['tax_rates'] = $this->sales_model->getAllTaxRates();
            $this->data['companies'] = $this->sales_model->getAllCompanies();
            $this->data['page_title'] = $this->lang->line("new_quote");
            $this->page_construct('sales/add_quote', $this->data);

        }
    }

Model:

public function addQuote($data = array(), $items = array(), $customer = array()) {

        if(!empty($customer)) {

            if($this->db->insert('customers', $customer)) {
                $customer_id = $this->db->insert_id();
            }
            $data['customer_id'] = $customer_id;
        }

        if($this->db->insert('quotes', $data)) { //Not inserted so Not enter into this loop

            $quote_id = $this->db->insert_id();

            foreach ($items as $item) {
                $item['quote_id'] = $quote_id;
                $this->db->insert('quote_items', $item);
            }
            return true;
        }
        else{
            print_r("not inserted DATA");
            exit;
        }

        return false;
    }

Array Result:(Print_r($data))

Array ( [reference_no] => Q-SMGP/17-18/000003 [company_id] => 1 [company_name] => SMGP [vehicle_no] => dfg [date_time_supply] => 2017-07-15 12:17 [place_supply] => sdafsd [consignee_name] => safsdaf [consignee_address] => sdfsdaf [consignee_gstin] => 6556 [consignee_state] => sdfsa [consignee_state_code] => sdafaf [date] => 2017-07-15 12:17 [due_date] => [expiry_date] => 2017-07-15 [user] => 1 [user_id] => 1 [customer_id] => 3 [customer_name] => Seed Arise [total_tax] => 28.0000 [total] => 2100 [grand_total] => 2600.0000 [status] => ordered [shipping] => 500.00 [note] => )

3
check for your model validation. You might getting validation error there. - urfusion
You're defining variables inside if statements, but are using them after, regardless if they are set or not. Then you're trying to insert the values without checking if they even exist. Check your error log. - M. Eriksson
Not exactly...Bcoz I tried Print_r inside the Form_validation()...and it perfectly works.. - Silambarasan R.D
@MagnusEriksson Error Log shows nothing bro - Silambarasan R.D
You should still redo your logic to handle different cases. Never assume that you're getting the correct data or that your method calls is successful. You should also dump the results as you go (in different places in your code) to see if it enters all the if's etc. - M. Eriksson

3 Answers

2
votes

Check if table fields name. Beasuse of wrong field name insert may be not working.

$this->db->last_query();

Use this to find. It will give you the sql query of insert. Run it in phpmyadmin.

2
votes

Solution (28-02-2019):

As per CI Docs (Queries)

$this->db->insert('quotes');
print_r($this->db->error());
exit;

This will show if any error occurs else return an empty array.

On my case it shows invoice_no can't be null

Old:

function add_quote()
    {

    $this->form_validation->set_rules('invoice_no', $this->lang->line("invoice_no")); //This line is the Main problem...
    $this->form_validation->set_rules('date', $this->lang->line("date"), 'required');
    $this->form_validation->set_rules('customer', $this->lang->line("customer"), 'required');
    if($this->input->post('customer') == 'new') {
        $this->form_validation->set_rules('state', $this->lang->line("state"));
        $this->form_validation->set_rules('gstin', $this->lang->line("gstin"));
        $this->form_validation->set_rules('company', $this->lang->line("company")." ".$this->lang->line("name"), 'required');
        $this->form_validation->set_rules('email', $this->lang->line("customer")." ".$this->lang->line("email_address"), 'required|valid_email|is_unique[customers.email]');
        $this->form_validation->set_rules('phone', $this->lang->line("phone"), 'required|min_length[6]|max_length[16]');
    }

$this->form_validation->set_rules('invoice_no', $this->lang->line("invoice_no"));

Mistakenly I just included this line...And I removed REQUIRED option...So it shows no error and do nothing..So when I removed this line, It perfectly working..anyway Thankyou all

0
votes

Try this way:

I have added two technique for quotes table insert query to check that data inserted or not.

public function addQuote($data, $items, $customer) {

    if(!empty($customer)) {

        if($this->db->insert('customers', $customer)) {
            $customer_id = $this->db->insert_id();
        }
        $data['customer_id'] = $customer_id;
    }


    if(!empty($data)){

        $this->db->insert('quotes', $data);
        $quote_id = $this->db->insert_id();

        // You can use one of this technique
        //one is the last inserted id
        // Second technique is described after this code

        if($quote_id > 0){

            foreach ($items as $single_item) {
                $single_item['quote_id'] = $quote_id;
                $this->db->insert('quote_items', $single_item);
            }
            return TRUE;

        } else {

            print_r("not inserted DATA");
            exit;

        }

    }       
    return FALSE;
}

This the second technique of $this->db->affected_rows() to check quotes tabke inserted a row or not.

// OR Second, You can use affected rows

if($this->db->affected_rows() == '1'){

    foreach ($items as $single_item) {
        $single_item['quote_id'] = $quote_id;
        $this->db->insert('quote_items', $single_item);
    }
    return TRUE;            
}   

I am assuming that you have taken all column name correctly. Codeigniter has a log system to see what happing in the system you can use that here.

 log_message('error', 'Last query executed you can write here regarding log message: '. print_r($this->db->last_query(), TRUE));

In order for the log file to actually be written, the logs/ directory must be writable. In addition, you must set the “threshold” for logging in application/config/config.php. You might, for example, only want error messages to be logged, and not the other two types. If you set it to zero logging will be disabled.