2015-03-08 41 views
1

UPDATE無法從iOS的

添加進入的MySQL儘管不同的崗位在這裏,我還沒有找到一個解決方案,加入我的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 [關鍵](如「$姓」)無法識別,因爲當我寫我json_array到文件,我看,關鍵是方括號裏面一樣,如果它是一個元素的數組:

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

這是一個正常的發現或可能解釋爲何我不能在我的SQL提取值聲明?即使我使用json_decode($ body,TRUE),我無法返回數組對象而不是stdClass對象。

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

我希望這可能是有用的...

回答

3

因爲我們正在處理字符串,在你的價值觀的變量需要他們被引用:

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

欲瞭解更多信息字符串常量,請訪問:

+0

感謝您的回覆,但不幸的是這並沒有成功。我在直接傳遞字符串時也遇到了同樣的問題(比如VALUES('string1','string2'等),它似乎在瀏覽器中運行腳本時工作,但不能從我的應用程序中運行...我以爲是一個obj-C錯誤,但我收到我的應用程序中的json_encode()響應,所以腳本也被執行了。我瘋了... – Trichophyton 2015-03-09 17:16:50

+0

實際上,它看起來像php://中的$ body(form POST方法)輸入是正確的,因爲它產生一個數組我可以json_encode()並返回到我的應用程序(我在UIAlertView中看到的結果),但這聽起來像sql語句不被調用,因爲它不工作。不是隻從我的應用程序調用,因爲我可以從默認字符串(VALUES('string1'...)我的瀏覽器添加mySQL數據庫中的條目我找不出什麼問題... – Trichophyton 2015-03-09 17:25:17

+0

@Trichophyton你'如果你可以發佈更多的代碼並且相對於你剛纔所說的話,它會幫助我或者其他跟蹤你的問題的人,希望給你正確的解決方案。 – 2015-03-09 17:27:10