2013-01-09 38 views
0

我試圖通過PHP腳本從iOS應用程序發送JSON數據到MySQL數據庫時遇到一些問題。iOS應用程序到MySQL數據庫與PHP不能正常工作

這裏是iOS的應用程序的代碼:

- (IBAction)sendButton:(id)sender 
{ 
NSString *currentDate = [[NSString alloc] initWithString:[self getDate]]; 
NSDictionary *newsData = [[NSDictionary alloc] initWithObjectsAndKeys:titleField.text, @"title", nyheterTextField.text, @"newsText", currentDate, @"date", nil]; 
NSError *error = [[NSError alloc] init]; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:newsData options:NSJSONWritingPrettyPrinted error:&error]; 
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
NSLog(jsonString); 
titleField.text = @""; 
nyheterTextField.text = @""; 
NSString *urlString = [NSString stringWithFormat:@"http://affectproductions.net/nyheter/upload.php"]; 

NSMutableData *body = [NSMutableData data]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 
NSString *boundary = @"---------------------------14737809831466499882746641449"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"json\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"%@",jsonString] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[request setHTTPBody:body]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
if (theConnection) { 
    self.recievedData = [NSMutableData data]; 
    NSLog(@"det funkade"); 
} else { 
    NSLog(@"NSURLConnection failed"); 
} 

,這裏是PHP腳本:

<html> 
<body> 

<?php 
$json = $_POST["json"]; 
$con = mysql_connect("xxx", "xxx", "xxx"); 

mysql_select_db("FreeSir_MarinaLaroverket") or die("Unable to select database"); 

$result = json_decode($json); 
foreach($result as $key => $value) { 
if($value) { 

mysql_query("INSERT INTO Nyheter (Title, News, Date) VALUES ($value->newsText, $value->title, $value->date)") or die ("error" . mysql_error()); 
echo "finito"; 
mysql_close($con); 
?> 

</body> 
</html> 

劇本是工作之前,我把它連接到我的iPhone應用程序,但現在沒有所有值都出現在mysql數據庫中。

問候

免費Sirenety

+0

請嘗試以下,而不是 – Srikanth

+0

能做到這一點,正要確保在清理之前確保一切正常從黑客安全;) 但是,無論如何感謝警告! – FreeSirenety

+0

@FreeSirenety如果你知道,那麼現在就可以了:) – 2013-01-09 20:25:58

回答

0

試試下面的不是:

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 

NSString *contentType = @"application/json"; 
+0

數據庫中仍然沒有增值:/ – FreeSirenety

相關問題