因此,我使用新代码修复了问题代码,并更改了问题的标题,供其他人使用。
我的问题是我在POST
方法中使用的应用程序内部,但在PHP
方面,我正在使用GET
方法。只需将其更改为POST
即可完美运行。
享受。
我的服务器一直收到用户名错误的错误,我在这里做错了什么?
感谢您的帮助。
- (void)addNewUser:(NSString *)username andDate:(NSDate *)date
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString * urlString = [NSString stringWithFormat:@"http://www.myurl.com/url/insert.php"];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:date];
NSString *paramDataString = [NSString stringWithFormat:@"username=%@&date=%@",[username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[strDate stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:[paramDataString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *requestData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];
}
在服务器端,我有:(PHP)
<?php
header("Content-type: text/html; charset=utf8");
$connection = mysql_connect("localhost","myusername","mypassword");
if(!$connection)
{
die("database connection failed:" . mysql_error());
}
$db = mysql_select_db("db_users",$connection);
if(!db)
{
die("database connection failed:" . mysql_error());
}
mysql_query("SET NAMES 'utf8'",$connection);
?>
<?php
$varGet1 = $_POST['username'];
$varGet2 = $_POST['date'];
if (!$varGet1) {
$message = "no username";
} else if (!$varGet2) {
$message = "no date";
} else {
$query = "INSERT INTO `Users` (username, date, first) VALUES('{$varGet1}','{$varGet2}','0')";
$query = mysql_query($query,$connection);
if($query) {
$message = "Added";
} else {
$message = "Error";
}
}
echo($message);
mysql_close($connection);
?>
答案 0 :(得分:1)
没有什么是错的。你只需从你的php GET
进行POST
来自objective-c。切换到$_POST
,你会很高兴。