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

How to pass variables while calling a function in codeIgniter

我正在尝试使用 codeIgniter 创建休息 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);
}

下面是我在模型中的函数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";
  }
}

我不明白哪里出了问题。我是 CodeIgniter 的新手,我对 java 有很好的了解。

我想学这个

请帮忙

谢谢!

尝试使用 codeigniter 数据库库。在 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";
  }
}

希望对您有所帮助。