2015-02-12 59 views
0

我正在嘗試使用codeIgniter創建其他api。我有一個方法,從android(客戶端)提供一個json對象。如何在調用codeIgniter中的函數時傳遞變量

下面是功能在我的控制器

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

您是否使用CodeIgniter REST服務器庫? – 2015-02-12 06:29:23

+0

是https://github.com/chriskacerguis/codeigniter-restserver/tree/master/application – Naveen 2015-02-12 06:30:55

+0

你是否解釋...究竟是什麼問題 – 2015-02-12 06:32:35

回答

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

希望這會有所幫助。

相關問題