无法从iOS添加进入MySQL的条目

时间:2015-03-08 22:16:52

标签: php ios mysql json post

更新

尽管这里有不同的帖子,我还没有找到在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](比如&#39; $ firstname&#39;)无法识别,因为当我将json_array写入文件时,我看到键位于方括号内,如果它是一个包含一个元素的数组:

stdClass Object
(
    [firstname] => Robert
    [eds] => 1234567
    [lastname] => Redford
    [dateOfBirth] => 12.01.1965
)

这是一个正常的发现还是可以解释为什么我无法在我的sql语句中提取值? 而且我不能返回数组对象而不是stdClass对象,即使我使用json_decode($ body,TRUE)。

我添加了使用POST方法发送字典的obj-C代码。这是调用上面的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.

我希望这可能有用......

1 个答案:

答案 0 :(得分:3)

由于我们正在处理字符串,因此VALUES中的变量需要引用它们:

VALUES ('$firstname', '$lastname', '$eds', '$dateOfBirth')

有关字符串文字的更多信息,请访问: