【问题标题】:Passing an array of ID's from Swift URLSession to PHP for an SQL query?将一组 ID 从 Swift URLSession 传递给 PHP 以进行 SQL 查询?
【发布时间】:2018-06-08 22:04:35
【问题描述】:

我有一个具有 ID 数组的 iOS 应用程序。这些 ID 都是数字:

var ids = [Int]() //Id array in my TableView
ids = basketStruct.getAllIds() //Pulling the array of ID's from my basket model.
print("This is how PHP sees the POST") //Debugging purposes
print(self.ids)
print("This is how PHP sees the POST")
basketServer.ids = self.ids //Passing the ID's to my URLSession.

URLSession 按以下方式处理数组:

var ids = [Int]()
func downloadItems() {
    request.httpMethod = "POST"
    let postParameters = "ids="+String(describing: ids)
}

我不再发布 URLSession 类,因为它可能没有必要。

现在我的 XCode 控制台如下:

This is how PHP sees the POST
[1,5,7,8]
This is how PHP sees the POST
Data downloaded
Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}

我没有返回任何结果。但是,如果我将所有 ID 数组更改为 NSArray 类型并运行,只要我的数组仅包含 1 个 ID,就会返回结果。第二次我将超过 1 个 ID 添加到数组中,我得到了同样的错误。

现在,在我的 php 上,如果我将所有内容都更改为 GET 而不是 POST,并直接在我的 URL 栏中输入一些值并转到浏览器中的页面,一切都会正常运行。我收到了一个不错的 JSON 格式响应,所有 ID 都列在我的 URL 栏中。

如果我将所有内容改回 POST,然后在尝试运行我的应用程序后访问我的 apache 错误日志文件,他们会说:

[Thu Feb 22 20:51:42.873663 2018] [:error] [pid 18793] [client 192.168.1.46:60790] PHP Fatal error:  Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[1])' at line 1 in /var/www/api/DbOperation.php:92\nStack trace:\n#0 /var/www/api/DbOperation.php(92): PDO->prepare('SELECT * FROM `...')\n#1 /var/www/api/Basket.php(14): DbOperation->basket('[1, 5, 7, 8]')\n#2 {main}\n  thrown in /var/www/api/DbOperation.php on line 92

我知道这个问题很广泛并且由很多部分组成,但我被困住了:(任何帮助将不胜感激。

谢谢

P.S - 万一有人想看,这是我的 PHP 代码:

这也是我的 URLSession 所在的 PHP 页面:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once dirname(__FILE__) . '/DbOperation.php';

$ids = $_POST["ids"];

$db = new DbOperation();
$json = $db->basket($ids);

}

这里是 DbOperation:

    public function basket($ids) {
    require dirname(__FILE__) .  '/../../dbconnect.php';
    $sql = "SELECT * FROM `Menu` WHERE `ID` IN ($ids)";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $result = $stmt->fetchAll();
    echo json_encode($result);
}

【问题讨论】:

    标签: php swift urlsession


    【解决方案1】:

    在 PHP 异常输出中,您可以看到使用如下字符串调用篮子函数:

    DbOperation->basket('[1, 5, 7, 8]')
    

    因此,PHP 代码会生成这样的 SQL:

    $sql = "SELECT * FROM `Menu` WHERE `ID` IN ($ids)";
    

    这是:

    SELECT * FROM `Menu` WHERE `ID` IN ([1, 5, 7, 8])
    

    这不是有效的 SQL。

    您可以通过将 $ids 字符串转换为适用于 SQL 的格式来完成这项工作。

    // Remove '[' and ']' characters
    $inValues = str_replace(['[',']'], '', $ids);
    $sql = "SELECT * FROM `Menu` WHERE `ID` IN ($inValues)";
    

    现在你会得到:

    SELECT * FROM `Menu` WHERE `ID` IN (1, 5, 7, 8)
    

    【讨论】:

    • 这很容易受到 SQL 注入的攻击。
    • 我爱你。我从 ishegg 那里明白了这一点,我已经知道这可能会受到注入攻击,我只是不明白为什么它没有按预期工作。我会把你标记为答案。谢谢你这么清楚的解释。
    猜你喜欢
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多