简体   繁体   中英

iOS application to mysql database with php not working properly

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

Here is the code of the iOS application:

- (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:

<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.

Best Regards

Free Sirenety

Try the following instead of:

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

do

NSString *contentType = @"application/json";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM