我在网络服务器上有一个简单的PHP脚本,无法让我的iOS应用程序向其发送数据并将数据保存在php数据库中。检查php数据库时没有新评论。我检查了很多网站,似乎无法找到我做错了什么。我认为这是在PHP代码本身。如果我添加表单并在浏览器上访问该网站,我就能够使用它。
PHP代码(服务器,用户名,密码都正确)
<!DOCTYPE html>
<html>
<head>
<title>Comment System</title>
</head>
<body>
<h1>Add a comment</h1>
</body>
<?php
$name = $_POST['name'];
$Judgename = "Judge Dredd";
$IP= $_SERVER['REMOTE_ADDR'];
$comment = $_POST['comment'];
if($name){
if($comment){
mysql_connect("Server","Username","Password");
mysql_select_db("comments");
mysql_query("INSERT INTO comments (id, name, comment, Judge, IP) VALUES ('','$name','$comment','$Judgename','$IP')");
}
else
echo "Empty Comment!";
}
else
echo "Empty Name!";
?>
</html>
以下是我的Xcode项目中的代码。
- (void)phptesting{
NSString *eventDescription = @"Hello2";
NSString *EventCity = @"Hello3";
NSString *myRequestString = [NSString stringWithFormat:@"&name=%@&comment=%@",eventDescription,EventCity];
NSLog(@"%@", myRequestString);
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");
}
}