I am trying to create a rest api using codeIgniter. I have a method which gives a json object from android (client).
Below is the function in my controller
function addUser_post(){
$username = $this->post('username');
$password = $this->post('password');
echo $username;
echo $password;
$this->load->model('register_model');
$data = $this->register_model->addUser($username,$password);
$this->response($data,200);
}
Below is my function in model class
function addUser($username, $password){
$uname = $username;
$passwo = $password;
$con = mysqli_connect('127.0.0.1', 'root', 'span123','naveendb');
$sql = "INSERT INTO usertable(username,password) VALUES('$uname','$passwo')";
if(!mysqli_query($con,$sql)){
return "user not added to the database";
}else{
return "user added";
}
}
I am not able to understand where is have went wrong. I am a newbie to CodeIgniter and i have good knowledge of java.
I want to learn this.
Please help
Thanks!
Try to use codeigniter db library. Set the database credentials in application/config/database.php
. Please reffer :Database Configuration.
function addUser($username, $password){
//If database class is not autoloaded then add the line below
$this->load->database();
$uname = $username;
$passwo = $password;
$data = array(
'username' => $uname ,
'password' => $passwo
);
if(!$this->db->insert('usertable', $data)){
return "user not added to the database";
}else{
return "user added";
}
}
Hope this helps.