Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
Try the following instead of – Srikanth Jan 9 at 19:58
Will do that, was just going to make sure that everything would work before cleaning up and making sure that it's safe from hackers ;) But thanks anyhow for the warning! – FreeSirenety Jan 9 at 20:16
@FreeSirenety If you're aware that, then it's OK for now :) – H2CO3 Jan 9 at 20:25
@H2CO3 Haha perfect :) But do yuo have any clue about what is wrong? :P – FreeSirenety Jan 9 at 20:45

1 Answer

Try the following instead of:

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

do

NSString *contentType = @"application/json";
share|improve this answer
Still no added value in the database :/ – FreeSirenety Jan 9 at 20:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.