Search code examples
codeigniter

A Database Error Occurred Error Number: 1146 in codeigniter


I am new to codeigniter where I tried form validation.

Here is my View Code:

<?php echo validation_errors(); ?>
          <?php echo form_open('enquiry'); ?>
          <table width="580" border="0" cellspacing="0" cellpadding="5">

            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *Full Name:<br /></td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="name" value="<?php echo set_value('name'); ?>" class="textbox-style01" /></td>
            </tr>
            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *Company: <span id="mndCompany"> </span><br /></td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="company" value="<?php echo set_value('company'); ?>"  class="textbox-style01" /></td>
            </tr>
            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *E-mail:</td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="email" value="<?php echo set_value('email'); ?>" class="textbox-style01" /></td>
            </tr>
            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *Phone: <br /></td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="phone" value="<?php echo set_value('phone'); ?>" class="textbox-style01" /></td>
            </tr>
            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *Fax: <br /></td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="fax" value="<?php echo set_value('comments'); ?>" class="textbox-style01" /></td>
            </tr>
            <tr>
              <td width="179" height="30" rowspan="2" align="right" valign="top" class="normal-text-style"> *Comments: <br /></td>
              <td width="421" height="12" align="left" valign="middle" class="normal-style"><textarea  name="comments" class="msgebox-style01"><?php echo set_value('comments'); ?></textarea></td>
            </tr>
            <tr>
              <td height="50" align="left" valign="top" class="normal-style"><input type="image" name="submit" src=" <?php echo base_url(); ?>/public/images/submit01.png" alt="submit" /></td>
            </tr>
          </table>
          </form>

And this is my controller

function index()
    {
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('company', 'Company', 'trim|required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('phone', 'phone', 'trim|required|xss_clean');
        $this->form_validation->set_rules('fax', 'fax', 'trim|required|xss_clean');
        $this->form_validation->set_rules('comments', 'comments', 'trim|required|xss_clean');


        $data['header_menu']=$this->load->view('header_menu', '', TRUE);
        $this->load->view('inner_header',$data);
        $data['left_menu']=$this->load->view('left_menu', '', TRUE);

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('enquiry',$data);
        }

        else
        {
           $this->load->view('enquiry/formsucess',$data);   
        }    
        $this->load->view('footer');
        //$this->load->view('enquiry',$data);


    }
    function formsucess()
    {


        $name = $this->input->post('name');
        $company = $this->input->post('company');
        $email = $this->input->post('email');
        $phone = $this->input->post('phone');
        $fax = $this->input->post('fax');
        $comments = $this->input->post('comments');
        $this->db->query("INSERT INTO `st_enquiry` 
        (name, company,email,phone,fax,comments,status,created_on)
        VALUES
        ('$name','$company','$email','$phone','$fax','$comments','1',now())");

        $data['header_menu']=$this->load->view('header_menu', '', TRUE);
        $this->load->view('inner_header',$data);
        $data['left_menu']=$this->load->view('left_menu', '', TRUE);
        $this->load->view('media',$data);
        $this->load->view('footer');


    }

After submitting the form I got an error like:

A Database Error Occurred

Error Number: 1146

Table 'table.users' doesn't exist

Solution

  • checkout this link

    Here

    $data = array(
       'title' => 'My title' , // column_name => value
       'name' => 'My Name' ,
       'date' => 'My date'
    );
    
    $this->db->insert('tablename', $data);
    

    must work in any condition.