About Lesson
In this article, we will discuss, How to create a Sign up with MySQL database using the CodeIgniter 3.
1. Create a controller file Signup.php in the applications/controllers/ directory.
class Signup extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('signup_model'); } public function sign_up() { $this->load->view(‘sign_up’); } public function register() { $name = $this->input->post('name'); $email = $this->input->post('email'); $password = $this->input->post('pass'); $phone = $this->input->post('phone'); $data = [ ‘name’ => $name, ‘email’ => $email, ‘password’ => $password, ‘phone’ => $phone, ]; $result = $this->signup_model->is_user_exists($email); if(!$result) { $user = $this->signup_model->add($data); if($user) { echo “Your account created successfully”; } else { echo “Something went wrong.”; } } else { echo “User is already exists.”; } } }
2. Create a model file Signin_model.php in the applications/models/ directory.
class Signup_model extends CI_Model { public function add($data) { return $this->db->insert(‘user_info’, $data); } public function is_user_exists($email) { $this->db->where([‘email’ => $email]); $query = $this->db->get(‘user_info’); return $query->num_rows(); } }
3. Create a sign_up.php file in the applications/views/ directory.
<form method="post" action="<?php echo base_url('user/register') ?>"> <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 Mobile</td> <td> <input type="text" name="phone" /> </td> </tr> <tr> <td>Enter Your Password</td> <td> <input type="password" name="pass" /> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="save" value="Register" /> </td> </tr> </table> </form>