【问题标题】:Json post method in swfit 3 for the below php codeswift 3中的Json post方法用于以下php代码
【发布时间】:2017-05-17 21:55:54
【问题描述】:

我有一个 PHP 代码,用于在注册时将用户值添加到数据库。我需要快速代码将用户名、电子邮件、密码等用户值插入数据库。我这样做过一次,但 PHP 代码使用了 POST 方法。如何以这种格式发帖。

<?php
function userReg($json_request){

    //DB connection details
    include 'connection.php';

    $serviceId = $json_request['requestHeader']['serviceId'];
    $fullname = $json_request['requestInput']['full name'];
    $emailId = $json_request['requestInput']['email'];
    $password = $json_request['requestInput']['password'];

    $queryUser = "SELECT * FROM user_master WHERE email = '".$emailId."'";

    $result_user = $conn->query($queryUser);
    if($result_user->num_rows == 0){

        $insertUserSql = "INSERT INTO user_master (user_name, email, password) VALUES ('".$fullname."', '".$emailId."', '".$password."')";

        if (mysqli_query($conn, $insertUserSql)){
            $getUserIdSql = "SELECT user_id FROM user_master WHERE email = '".$emailId."'";
            $result_userId =  $conn->query($getUserIdSql);
            while($row_user = $result_userId->fetch_assoc()) {
                $user_details[] = $row_user;
            }
            $userId = $user_details["0"]["user_id"];

            $res['responseHeader']['serviceId'] = $serviceId;
            $res['responseHeader']['status']  = "100";
            $res['responseHeader']['message'] = "Success";
            $res['registerUserOutput']['userID'] = $userId;
            $res['registerUserOutput']['userInfo']['fullName'] = $fullname;
            $res['registerUserOutput']['userInfo']['email'] = $emailId;
            $res['registerUserOutput']['userInfo']['profilePic'] = "";

            $json_user_output = json_encode($res, JSON_UNESCAPED_SLASHES);
            echo $json_user_output;  
        }
    }
    else{
        $res['responseHeader']['serviceId'] = $serviceId;
        $res['responseHeader']['status']  = "99";
        $res['responseHeader']['message'] = "Email ID already exists";
        $res['registerUserOutput'] = "{}";

        $json_user_output = json_encode($res, JSON_UNESCAPED_SLASHES);
        echo $json_user_output;
    }
}

?>

【问题讨论】:

  • 你可以在这里关注我的回答:stackoverflow.com/questions/43907542/…它将帮助你为你的PHP代码编写post方法
  • @Rouny 它不工作
  • 您是否将用户名以及电子邮件和密码添加到该 post 方法中,并确保将代码的状态代码更改为 100,并尝试使用有效凭据在邮递员中测试 API 一次。
  • 正如@Rouny 所说,首先检查您的注册 API 路由是否正在使用邮递员工作。然后您可以使用 Alamofire 向 php 服务器发出 post 请求。

标签: ios swift


【解决方案1】:

此答案涵盖如何将参数添加到 URL 会话的发布请求部分:POST Request in swift with key-value coding

现在我想对您的 PHP 代码做一点评论。我发现您执行 SQL 的方式存在 2 个问题。 首先,我建议使用PDO 代替(有用的博客文章:PDO vs. MySQLi

其次,最重要的是,您将用户生成的值直接插入到查询中,从而使您直接容易受到SQL注入攻击.

解决这个问题的正确方法是在 PDO 中使用准备好的语句。虽然您可以使用mysqli_real_escape_string(),但通常只使用准备好的语句更容易更好。由于这些转义函数(尤其是 addslashes())可能存在自身的漏洞,并且使您的代码的可读性低于准备好的语句。

这会是这样的:

<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

来源:PDO Prepared statements

我已经有一段时间没有做过任何原始 PHP 了,所以这对我来说是最重要的,但应该可以帮助你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2017-02-24
    • 2011-12-01
    • 1970-01-01
    • 2017-02-18
    相关资源
    最近更新 更多