【发布时间】: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