0
我覺得這不應該是這麼複雜,但在這之後我花了兩天的時間試圖讓它工作。我試圖從我的iOS應用程序發送兩個變量到我在服務器上的一個php腳本。該數據庫添加一個條目,但2個信息字段爲空。我沒有客觀的C但是PHP新手。從iOS應用程序發送數據到PHP腳本
PHP代碼
<?php
$name = $_POST['name'];
$comment = $_POST['comment'];
mysql_connect("Server","Username","Password");
mysql_select_db("comments");
mysql_query("INSERT INTO comments (id, name, comment) VALUES ('','$name','$comment')");
?>
目標C代碼
NSString *name = @"Name1";
NSString *comment = @"Comment1";
NSString *myRequestString = [NSString stringWithFormat:@"&name=%@&comment=%@",name,comment];
NSData *myRequestData =[myRequestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[myRequestData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.notguiltyapp.com/harris.php"]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
}
else
{
NSLog(@"Connection could not be made");
}
取代它沒有工作。你爲什麼要爲每個變量添加引號? – user3157910