不在CODEIGNITER Mysql中插入数据库

时间:2017-07-15 08:17:04

标签: php mysql arrays database codeigniter

不插入..任何建议?? DB驱动程序:mysqli 使用Codeigniter。

控制器

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);

        }
    }

型号:

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;
    }

数组结果:(Print_r($ data))

  

数组([reference_no] =&gt; Q-SMGP / 17-18 / 000003 [company_id] =&gt; 1   [company_name] =&gt; SMGP [vehicle_no] =&gt; dfg [date_time_supply] =&gt;   2017-07-15 12:17 [place_supply] =&gt; sdafsd [consignee_name] =&gt; safsdaf   [consignee_address] =&gt; sdfsdaf [consignee_gstin] =&gt; 6556   [consignee_state] =&gt; sdfsa [consignee_state_code] =&gt; sdafaf [date] =&gt;   2017-07-15 12:17 [due_date] =&gt; [expiry_date] =&gt; 2017-07-15 [user] =&gt; 1   [user_id] =&gt; 1 [customer_id] =&gt; 3 [customer_name] =&gt;种子起来   [total_tax] =&gt; 28.0000 [总] =&gt; 2100 [grand_total] =&gt; 2600.0000   [status] =&gt;已订购[shipping] =&gt; 500.00 [注意] =&gt; )

3 个答案:

答案 0 :(得分:2)

检查表字段是否有名称。错误的字段名称插入可能无法正常工作。

$this->db->last_query();

用它来查找。它将为您提供insert的sql查询。在phpmyadmin中运行它。

答案 1 :(得分:1)

解决方案(28-02-2019):

根据CI文档(Queries

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

这将显示是否发生任何错误,否则返回一个空数组。

  

在我的情况下,它显示 invoice_no不能为空

<强>旧

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

$这 - &GT; form_validation-&GT; set_rules( 'invoice_no',   $这 - &GT;朗&GT;线( “invoice_no”));

我错误地把这条线包括在内......而且我删除了必需的选项...所以它没有显示任何错误,什么也没做。所以当我删除这一行时,它完全正常工作..所以谢谢你们所有

答案 2 :(得分:0)

尝试这种方式:

我为quotes表插入查询添加了两种技术来检查是否插入了数据。

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->db->affected_rows()检查quotes tabke的第二种技巧是否插入了一行。

// 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;            
}   

我假设您已正确使用所有列名称。 Codeigniter有一个日志系统,可以看到你可以在这里使用的系统。

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

为了实际写入日志文件,logs/ directory必须是可写的。此外,您必须设置登录application/config/config.php的“阈值”。例如,您可能只希望记录错误消息,而不是其他两种类型。如果将其设置为零,则将禁用日志记录。