gpt4 book ai didi

php - 从 iOS 向 PHP 服务器发送 POST 请求?

转载 作者:行者123 更新时间:2023-11-28 23:39:37 27 4
gpt4 key购买 nike

当我尝试使用 PHP 服务器注册我的用户表单 iOS 时,我总是在 mysql 数据库中添加空行。

当我从 php 打印变量值时,它是空的。

这是我的代码

-(void)sendLogininfoToServer{
NSUserDefaults *login = [NSUserDefaults standardUserDefaults];
NSString *name=[login stringForKey:@"name"];
//NSString *image=[login stringForKey:@"image"];
NSString *email=[login stringForKey:@"email"];


serverConnection=[[serverRequestProtocol alloc]init];
serverConnection.delegate=self;
//NSLog(@"get doc list%@",stringValue);
NSString *url=@"http://example.in/php/signup.php";
NSString *post = [NSString stringWithFormat:@"fname=%@&email=%@&country=%@&city=%@",name,email,@"India",@"Bangalore"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: url]];
NSString *serverOutput=[serverConnection getData:request :@"POST":postData];

NSLog(@"%@",serverOutput);

}



-(NSString *)getData:(NSMutableURLRequest *)request:(NSString *)requestType:(NSData *)data{
[request setHTTPMethod:requestType];
NSInteger millisecondsFromGMT = 1000 * [[NSTimeZone localTimeZone] secondsFromGMT];
NSString *str1=[NSString stringWithFormat:@"%ld",millisecondsFromGMT];
[request setHTTPBody:data];
[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"iphone" forHTTPHeaderField:@"X-User-agent"];
[request setValue:str1 forHTTPHeaderField:@"X-TimeZoneOffset"];
NSURLResponse *response;
NSError *err;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString * serverOutput= [[NSString alloc]initWithData:returnData encoding:NSASCIIStringEncoding];
return serverOutput;

}

我从 php 结果中获得成功,但总是空值被添加到 ginto db。

我的 php 代码

<?php
header("Content-type: text/html; charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dbname";
$charset="UTF8";
$name=$_POST['fname'];
$email=$_POST['email'];
$country=$_POST['country'];
$city=$_POST['city'];

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


/* change character set to utf8 */
if (!mysqli_set_charset($conn, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($conn));
exit();
} else {
mysqli_close($link);$sql = "INSERT INTO `basicinfo`(`fname`, `email`,`country`, `city`) VALUES ('$name','$email','$country','$city')";
if ($conn->query($sql) === TRUE) {
echo $name;
} else {
echo "fail";
}
}

exit;

?>

请帮帮我

数据库截图 enter image description here

最佳答案

尝试将您的 php 代码更改为以下内容(只是检查您是否正在获取 post 值):

<?php
header("Content-type: text/html; charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dbname";
$charset="UTF8";

if((isset($_POST['fname']))&&(isset($_POST['email']))&&(isset($_POST['country']))&&(isset($_POST['city'])))
{
$name=$_POST['fname'];
$email=$_POST['email'];
$country=$_POST['country'];
$city=$_POST['city'];

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


/* change character set to utf8 */
if (!mysqli_set_charset($conn, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($conn));
exit();
} else {
mysqli_close($link);$sql = "INSERT INTO `basicinfo`(`fname`, `email`,`country`, `city`) VALUES ('$name','$email','$country','$city')";
if ($conn->query($sql) === TRUE) {
echo $name;
} else {
echo "fail";
}
}
}
else
{
echo "no post values found!";
}

exit;

?>

如果您没有正确发送值,此代码将返回错误消息(“未找到发布值!”)。

关于php - 从 iOS 向 PHP 服务器发送 POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34750216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com