如何使用php从ios app在mysql中存储数据?

时间:2017-01-11 04:58:30

标签: php ios mysql

我想使用PHP在我的ios应用程序(使用swift 3)中保存用户注册详细信息。但它不起作用。 这是我的代码:

registeruser.php

$response = array ();
    if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
    // getting values
    $username = $_POST ["username"];
    $password = $_POST ['password'];
    // including the db operation file
    require_once '../include/DbOperation.php';
    // inserting values
    if ($db->createUser($username, $password)) {
        $response ['error'] = false;
        $response ['message'] = 'user added successfully';
    } else {
        $response ['error'] = true;
        $response ['message'] = ' user not added';
    }
echo json_encode ( $response );
 public function createUser($username, $password)
    {
$stmt = $this->conn->prepare("INSERT INTO registerUser(username, password) values(?, ?)");
        $stmt->bind_param("register", $username, $password);  
        $result = $stmt->execute();
        $stmt->close();
        echo $result;
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

我不知道为什么它不起作用?

错误显示为

  

未定义的索引:用户名和密码。

1 个答案:

答案 0 :(得分:0)

public function actionXXX() 
{
    $response = array ();
    if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
        // getting values
        $username = $_POST ["username"];
        $password = $_POST ['password'];
        // including the db operation file
        require_once '../include/DbOperation.php';
        // inserting values
        // $db = new ***
        if ($db->createUser($username, $password)) {
            $response ['error'] = false;
            $response ['message'] = 'user added successfully';
        } else {
            $response ['error'] = true;
            $response ['message'] = 'user not added';
        }
        return json_encode($response);
    }
}
public function createUser($username, $password)
{
    $stmt = $this->conn->prepare("INSERT INTO registerUser(username, password) values(?, ?)");
    $stmt->bind_param("register", $username, $password);
    $result = $stmt->execute();
    $stmt->close();
    if ($result) {
        return true;
    } else {
        return false;
    }
}