无法从iOS向MySQL添加条目


unable to add entry into MySQL from iOS

>UPDATE

尽管这里有不同的帖子,但我还没有找到在我的 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)。

我添加了使用 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.

我希望这可能有用...

由于我们正在处理字符串,因此 VALUES 中的变量需要将它们括起来:

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

有关字符串文本的详细信息,请访问:

  • http://dev.mysql.com/doc/refman/5.1/en/string-literals.html