我正在尝试学习如何为php文档创建一个http请求,然后将其插入到mysql表中。问题是它没有返回任何东西。没有错误,甚至没有mysql中的空行。我做错了什么?
目标c
NSString *post = [NSString stringWithFormat:@"name=%@&id=%@&userid=%@",@"lol", @"2", @"1"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.ratemyplays.com/api/post_user.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
php代码:
<?php
$link = mysqli_connect("mysql12.gigahost.dk","dirts","password_example","dirts_api");
$name = (string)$_POST['name'];
$id = (int)$_POST['id'];
$userid = (int)$_POST['userid'];
$query = "INSERT INTO User_friends (name, profileID, userID) VALUES ('$name', '$id', '$userid')";
$result = mysqli_query($link,$query);
?>
答案 0 :(得分:0)
如何添加提交然后用php检查
NSString *post = [NSString stringWithFormat:@"name=%@&id=%@&userid=%@submit=",@"lol", @"2", @"1"];
<?php
if (isset($_POST['submit'])
{
$link = mysqli_connect("mysql12.gigahost.dk","dirts","password_example","dirts_api");
$name = (string)$_POST['name'];
$id = (int)$_POST['id'];
$userid = (int)$_POST['userid'];
$query = "INSERT INTO User_friends (name, profileID, userID) VALUES ('$name', '$id', '$userid')";
$result = mysqli_query($link,$query);
if (!$result)
{
die(mysql_error());
}
?>