Issue
I'm a beginner in CodeIgniter and I'm trying to insert the value that I'm getting from the post into my database, for which I'm using the following code:
View Class:
<form method="POST">
<label>Username</label>
<input type="text" name="username"/>
<br/>
<label>Password</label>
<input type="password" name="password"/>
<br/>
<button type="submit" name="login">Sign In</button>
</form>
Controller Class:
class Auth extends CI_Controller{
public function index(){
$this->load->model("membership_model");
$username=$this->input->post('username');
$password=$this->input->post('password');
$content='';
return $this->load->view('auth/login');
if(isset($_POST["username"]) && isset($_POST["password"])){
$content['data'] = $this->membership_model->create_user($username,$password);
}
}
Model Class:
class Membership_model extends CI_Model {
public function create_user($username,$password){
$this->db->set('display_name',$username);
$this->db->set('password',$password);
$this->db->insert("table1");
return true;
}
}
But when I hit submit, I do not get any data in my database for some reason.
Thanks in advance.
Solution
The following code should work well.
Controller modification:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
class Auth extends Controller{
public function index(){
if($this->input->method() == "post") {
$this->load->model("membership_model");
$username=$this->input->post('username');
$password=$this->input->post('password');
$content='';
if(isset($_POST["username"]) && isset($_POST["password"])){
$content['data'] = $this->membership_model->create_user($username,$password);
}
}
return $this->load->view('auth/login');
}
Model modification:
public function create_user($username, $password){
$data = array(
'display_name'=>$username,
'display_name'=>$password
);
$this->db->insert("table1", $data);
return ($this->db->affected_rows() != 1) ? false : true;
}
Answered By - Raphael
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.