gpt4 book ai didi

php - 不使用 Objective C 向 PHP POST web 服务提交数据

转载 作者:行者123 更新时间:2023-11-29 02:22:19 25 4
gpt4 key购买 nike

我正在尝试从 php 网络服务取回数据以检查登录详细信息。但是,Objective C 代码似乎没有正确提交数据......请在下面找到我的 Objective C 和 PHP 代码

- (IBAction)doLogInPressed:(UIButton *)sender {
NSString *noteDataString = [NSString stringWithFormat:@"username=%@&password=%@", self.logInUsername.text, self.logInPassword.text];

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];


NSURL * url = [NSURL URLWithString:@"http://www.simonsayssolutions.co.uk/giftlistapi/checkLogin.php"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[noteDataString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) {
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:dataRaw
options:kNilOptions error:&error];
NSString *status = [json objectForKey:@"id"];

if(status.integerValue > 1){
self.logInUserCentreConstraint.constant = self.view.frame.size.height;
self.user = [[glUser alloc] init];
self.user.dbId = [json objectForKey:@"id"];
self.user.username = [json objectForKey:@"username"];
self.user.password = [json objectForKey:@"password"];
self.user.forename = [json objectForKey:@"firstname"];
self.user.surname = [json objectForKey:@"surname"];
self.user.email = [json objectForKey:@"email"];
[self performSegueWithIdentifier:@"Show Main Menu" sender:self];
} else {
}

}];
[dataTask resume];
}

这是它调用的 php 网络服务...

<?php

if (isset ($_POST["username"])){
$username = $_POST["username"];
$username = $_POST["password"];
$link = mysql_connect('www.simonsayssolutions.co.uk:3306','simonsay_root', 'PASSWORD') or die ('Cannot connect to DB');
mysql_select_db('simonsay_giftlist', $link) or die ('Cannot select db');
$query = "SELECT * FROM user where (username = '$username' AND password = '$password');";
$result = mysql_query($query, $link) or die ("Could not execute query");
$myArray = array();
header('Content-type:application/json');
while($user = mysql_fetch_array($result))
{
$myArray[] = $user;
}
echo json_encode($myArray);

} else {
echo "no data!";
}

?>

最佳答案

<?php

if (isset ($_POST["username"])){
$username = $_POST["username"];
$password = $_POST["password"];
$link = mysql_connect('www.simonsayssolutions.co.uk:3306','simonsay_root', 'bradicus1') or die ('Cannot connect to DB');
mysql_select_db('simonsay_giftlist', $link) or die ('Cannot select db');
$query = "SELECT * FROM user where (username = '$username' AND password = '$password');";
$result = mysql_query($query, $link) or die ("Could not execute query");
$myArray = array();

while($user = mysql_fetch_array($result))
{
$myArray[] = $user;
}
echo json_encode($myArray);

} else {
echo "no data!";
}

?>

关于错误:

  1. $用户名 = $_POST["密码"];

  2. header('Content-type:application/json');

    请记住,在发送任何实际输出之前必须调用 header(),无论如何在这种情况下不需要。

关于php - 不使用 Objective C 向 PHP POST web 服务提交数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27998431/

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