Reputation: 449
I am trying to get data back from a php web service to check log-in details. However, the Objective C code does not seem to be submitting the data correctly... please find below my Objective C and PHP code
- (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];
}
and here is the php web service it calls...
<?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!";
}
?>
Upvotes: 0
Views: 318
Reputation: 156
<?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!";
}
?>
About the errors :
$username = $_POST["password"];
header('Content-type:application/json');
Remember that header() must be called before any actual output is sent,anyway isn't required in this case.
Upvotes: 1