iOS应用程序到mysql数据库用php无法正常工作

时间:2022-02-28 17:00:42

I'm having some problems when trying to send JSON data from an iOS application to a mysql database via a php script.

尝试通过php脚本将iOS应用程序中的JSON数据发送到mysql数据库时,我遇到了一些问题。

Here is the code of the iOS application:

以下是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");
}

and here is the php script:

这是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>

The script was working before i connected it to my iOS application, but now no values at all appears in the mysql database.

在我将它连接到我的iOS应用程序之前,该脚本正在工作,但现在mysql数据库中根本没有任何值。

Best Regards

Free Sirenety

1 个解决方案

#1


0  

Try the following instead of:

尝试以下代替:

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

do

NSString *contentType = @"application/json";

#1


0  

Try the following instead of:

尝试以下代替:

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

do

NSString *contentType = @"application/json";