我正在尝试使用codeIgniter创建一个rest api。我有一个方法,从android(客户端)提供一个json对象。
以下是我的控制器中的功能
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);
}
以下是我在模型类中的功能
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";
}
}
我无法理解哪里出了问题。我是CodeIgniter的新手,我对java有很好的了解。
我想学习这个。
请帮忙
谢谢!
答案 0 :(得分:0)
尝试使用codeigniter db库。在application/config/database.php
中设置数据库凭据。请参阅: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";
}
}
希望这有帮助。