【发布时间】:2015-05-10 01:15:05
【问题描述】:
更新
尽管这里有不同的帖子,但我还没有找到在我的 MySQL 表中添加条目的解决方案。我从 iOS 应用程序发布了一个 json 字典,我想将它输入到数据库中。它由一个带有 4 个字段(名字、姓氏...)的条目组成。下面是服务器端的 php 代码(使用 MAMP 在本地运行):
<?php
DEFINE('DB_USERNAME', 'root');
DEFINE('DB_PASSWORD', 'root');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_DATABASE', 'syncList');
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno(). ') ' . mysqli_connect_error());
}
$body = file_get_contents('php://input');
$jsonArray = json_decode($body);
echo json_encode($jsonArray); // ---> send back for testing.
$firstname = $jsonArray['firstname'];
$firstname = $mysqli->real_escape_string($firstname);
$lastname = $jsonArray['lastname'];
$lastname = $mysqli->real_escape_string($lastname);
$eds = $jsonArray['eds'];
$dateOfBirth = $jsonArray['dateOfBirth'];
$sql = "INSERT INTO tbl_syncList (firstname, lastname, EDS, dateOfBirth) VALUES ('$firstname', '$lastname', '$eds', '$dateOfBirth')";
if ($mysqli->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $mysqli->error;
}
$mysqli->close();
?>
问题似乎是我的 json_array[key](如 '$firstname')无法识别,因为当我将 json_array 写入文件时,我看到密钥在方括号内,就像它是一个数组一个元素:
stdClass Object
(
[firstname] => Robert
[eds] => 1234567
[lastname] => Redford
[dateOfBirth] => 12.01.1965
)
这是一个正常的发现还是可以解释为什么我不能在我的 sql 语句中提取值?而且我不能返回一个数组对象而不是一个 stdClass 对象,即使我使用 json_decode($body, TRUE)。
我添加了 obj-C 代码,使用 POST 方法发送我的字典。这是调用上面的php脚本。
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:nil delegateQueue: [NSOperationQueue mainQueue]];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"Robert" forKey:@"firstname"];
[dictionary setValue:@"Redford" forKey:@"lastname"];
[dictionary setValue:@"1234567" forKey:@"eds"];
[dictionary setValue:@"12.01.1965" forKey:@"dateOfBirth"];
NSError *error;
//serialize the dictionary data as json
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString *urlString = @"http://192.168.1.106:8888/postPerson.php";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:data]; //set the data as the post body
[urlRequest addValue:@"postValues" forHTTPHeaderField:@"METHOD"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue:[NSString stringWithFormat:@"%d",data.length] forHTTPHeaderField:@"Content-Length"];
NSURLSessionDataTask *dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *dataRaw, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:dataRaw
options:kNilOptions error:&error];
NSString *result = [NSString stringWithFormat:@"%@", json];
if (error) {
UIAlertView * av = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Got error %@.\n", error] message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[av show];
}
UIAlertView *av = [[UIAlertView alloc] initWithTitle:result message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[av show]; // --> this is to check the array sent with POST-method which is json_encode() in the php script above.
我希望这可能有用...
【问题讨论】: