2017-07-15 30 views
1

没有插入..任何建议? DB驱动程序:mysqli 使用Codeigniter。在CODEIGNITER中没有插入数据库Mysql

控制器

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($数据))

阵列([reference_no] => Q-SMGP/17-18/000 003 [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] =>种子出现 [total_tax] => 28.0000 [total] => 2100 [grand_total] => 2600.0000 [status] => [shipping] => 500.00 [note] =>)

+0

检查您的模型验证。你可能会在那里得到验证错误。 – urfusion

+0

您正在if语句中定义变量,但在之后使用它们,无论它们是否设置。然后你试图插入值而不检查它们是否存在。检查你的错误日志。 –

+0

不完全... Bcoz我试过Form_validation()内的print_r ...和它完美的作品.. –

回答

2

检查表字段名称。错误的字段名称插入可能不起作用。

$this->db->last_query(); 

用此找到。它会给你插入的SQL查询。在phpmyadmin中运行它。

+0

我检查了很多次,但它不会工作.. :( –

+0

你在上面给出的数组[=]是空的,导致错误。 –

+0

它不是一个问题...我只是将表单验证包括到匿名输入字段和我删除了要求。所以它不能提交表单并且还没有显示出错误... 错误行: '$这个 - > form_validation-> set_rules(“invoice_no”,$这个 - >朗>线( “invoice_no”));' 解决: '$这 - > form_validation-> set_rules( 'invoice_no',$这 - >朗>线( “invoice_no”), '必要');' –

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的“阈值”。例如,您可能只想记录错误消息,而不是其他两种类型。如果您将其设置为零,则日志记录将被禁用。

0

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

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

误我只包含这条线......而我除去必需的选项......所以,也没有任何错误,做nothing..So当我删除了这条线,它完美地working..anyway三江源所有