Home >>Codeigniter Tutorial >CodeIgniter Insert Data into Database
Table Name : student | |
student_id | int primary key auto_increment |
name | char(50) |
varchar(100) | |
password | varchar(100) |
mobile | bigint |
course | char(100) |
<?php class User extends CI_Controller { public function __construct() { parent::__construct(); $this->load->database(); $this->load->helper('url'); } public function index() { if($this->input->post('register')) { //first check user's email id already exists or not $query=$this->db->get_where("student",array('email'=>$this->input->post('email'))); //count number of matching rows $row=$query->num_rows(); if($row) { $data['error']="<h3 style='color:red'>This user already exists</h3>"; } else { $data=array('student_id'=>'', 'name'=>$this->input->post('name'), 'email'=>$this->input->post('email'), 'password'=>$this->input->post('pass'), 'mobile'=>$this->input->post('mobile'), 'course'=>$this->input->post('course')); $this->db->insert('student',$data); $data['error']="<h3 style='color:blue'>Your account created successfully</h3>"; } } $this->load->view('student_registration',@$data); } } ?>
<!DOCTYPE html> <html> <head> <title>Student Registration form</title> </head> <body> <form method="post"> <table width="600" align="center" border="1" cellspacing="5" cellpadding="5"> <tr> <td colspan="2"><?php echo @$error; ?></td> </tr> <tr> <td width="230">Enter Your Name </td> <td width="329"><input type="text" name="name"/></td> </tr> <tr> <td>Enter Your Email </td> <td><input type="text" name="email"/></td> </tr> <tr> <td>Enter Your Password </td> <td><input type="password" name="pass"/></td> </tr> <tr> <td>Enter Your Mobile </td> <td><input type="text" name="mobile"/></td> </tr> <tr> <td>Select Your Course </td> <td> <select name="course"> <option value="">Select Course</option> <option>PHP</option> <option>Java</option> <option>Wordpress</option> </select> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="register" value="Register Me"/></td> </tr> </table> </form> </body> </html>