使用php的mysql数据库的iOS应用程序工作不正常
我在尝试通过php脚本将JSON数据从iOS应用程序发送到mysql数据库时遇到了一些问题 以下是iOS应用程序的代码:使用php的mysql数据库的iOS应用程序工作不正常,php,mysql,ios,json,post,Php,Mysql,Ios,Json,Post,我在尝试通过php脚本将JSON数据从iOS应用程序发送到mysql数据库时遇到了一些问题 以下是iOS应用程序的代码: - (IBAction)sendButton:(id)sender { NSString *currentDate = [[NSString alloc] initWithString:[self getDate]]; NSDictionary *newsData = [[NSDictionary alloc] initWithObjectsAndKeys:titleFiel
- (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>
在我将该脚本连接到iOS应用程序之前,它就已经工作了,但现在mysql数据库中根本没有显示任何值
致意
免费汽笛尝试以下方法,而不是:
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
做
尝试下面的方法,而不是那样做,只是为了在清理之前确保一切正常,并确保它不会受到黑客攻击;)但无论如何,谢谢你的警告@如果你意识到了这一点,那么现在还可以:)@H2CO3哈哈,太完美了:)但是你有什么不对劲的线索吗PStill数据库中没有附加值:/
NSString *contentType = @"application/json";