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"> *Full Name:<br /></td> <td height="30" align="left" valign="middle"><input type="text" name="name" value="<?php echo set_value('name'); ?>" /></td> </tr> <tr> <td height="30" align="right" valign="middle"> *Company: <span> </span><br /></td> <td height="30" align="left" valign="middle"><input type="text" name="company" value="<?php echo set_value('company'); ?>" /></td> </tr> <tr> <td height="30" align="right" valign="middle"> *E-mail:</td> <td height="30" align="left" valign="middle"><input type="text" name="email" value="<?php echo set_value('email'); ?>" /></td> </tr> <tr> <td height="30" align="right" valign="middle"> *Phone: <br /></td> <td height="30" align="left" valign="middle"><input type="text" name="phone" value="<?php echo set_value('phone'); ?>" /></td> </tr> <tr> <td height="30" align="right" valign="middle"> *Fax: <br /></td> <td height="30" align="left" valign="middle"><input type="text" name="fax" value="<?php echo set_value('comments'); ?>" /></td> </tr> <tr> <td width="179" height="30" rowspan="2" align="right" valign="top"> *Comments: <br /></td> <td width="421" height="12" align="left" valign="middle"><textarea name="comments"><?php echo set_value('comments'); ?></textarea></td> </tr> <tr> <td height="50" align="left" valign="top"><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 44 Answers
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.
$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[users.email]'); Here will be some change
is_unique[users.email]
01.here users mean your table name
02. email means email column in your table.
so it will be
$this->form_validation->set_rules('email','Email','required|valid_email| is_unique[table name.email column in your table]'); 1A Database Error Occurred
Error Number: 1146
Table 'table.users' doesn't exist
I solved this problem in Codeigniter by changing name of table in phpmyadmin i hope this can help you
Create a function in Model like:
$this->db->insert($this->table_name,$db_data); return $this->db->insert_id(); and send data from Controller to Model