如何在调用codeIgniter中的函数时传递变量


How to pass variables while calling a function in codeIgniter

我正在尝试使用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有很好的了解。

我想学这个。

请帮助

谢谢!

尝试使用codeigniter数据库库。在application/config/database.php中设置数据库凭据。请参考:数据库配置。

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";
  }
}

希望这能有所帮助。