It has been about 4 years since I last wrote PHP code. I am trying to write a web service that would insert into a MySQL database table. I'm passing the data via POST. In my app I am using NSURLConnection and it is initing ok with the NSURLRequest. But then I am getting a timeout error. When I check the db table it's cardinality is still 0 so I know the data is not being inserted. And I am not getting any hits on the NSURLConnection delegate methods. My suspicion is that there is something wrong with my PHP code. Could someone take a look at this and let me know if they see something odd.
I know the URL is correct. I also know the POST data is being attached to the request ok.
Here's the iOS code:
- (BOOL) submitAccountData : (NSArray*) arrAccountTextFields {
BOOL success = YES;
//set up url
NSURL* accountURL = [NSURL URLWithString:kAccountService];
NSMutableURLRequest* accountURLRequest = [NSMutableURLRequest requestWithURL:accountURL];
NSMutableDictionary* dictPostData = [[NSMutableDictionary alloc] init];
for(KIP_TextField* thisTextField in arrAccountTextFields) {
NSString* strKey = thisTextField.placeholder;
strKey = [strKey stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
strKey = [strKey stringByReplacingOccurrencesOfString:@" " withString:@""];
if (thisTextField.myTextFieldType == SECURE_TEXTFIELD) {
strKey = [strKey stringByReplacingOccurrencesOfString:@"word" withString:@""];
}
[dictPostData setValue:thisTextField.text forKey:strKey];
}
//add post data
accountURLRequest = [self postData:accountURLRequest :dictPostData];
//uncomment below to see what post data is being sent
//NSString* strPOSTData = [NSString stringWithUTF8String:(char*)[[accountURLRequest HTTPBody] bytes]];
//NSLog(@"%@", strPOSTData);
NSURLConnection* myURLConnection = [[NSURLConnection alloc] initWithRequest:accountURLRequest delegate:self];
if(myURLConnection) {
[myURLConnection start];
} else {
NSLog(@"Connection could not be made");
BOOL success = NO;
}
return success;
}
Here's the PHP code.
<?php
//get passed variables
$userName = $_POST['UserName'];
$userCity = $_POST['UserCity'];
$userState = $_POST['UserState'];
$userStateAbbrev = $_POST['UserStateAbbrev'];
$userEmail = $_POST['UserEmail'];
$userPass = $_POST['UserPass'];
//put into an array
$arrUserInfo = arrasy($userName, $userCity, $userState, $userStateAbbrev, $userEmail, $userPass);
$servername = "ramjamlaxcom1.ipagemysql.com";
$username = "mpl_admin";
$password = "mpl_admin";
$dbname = "mpl_main";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
echo "Connection failed!";
die("Connection failed: " . $conn->connect_error);
}
mysql_select_db(mpl_main);
//create prepared statement
$stmt = $dbh->prepare("INSERT INTO accounts (userName, userCity, userState, userStateAbbrev, userEmail, userPass) VALUES (:userName, :userCity, :userState, :userStateAbbrev, :userEmail, :userPass)");
$stmt->bindParam(':userName', $userName);
$stmt->bindParam(':userCity', $userCity);
$stmt->bindParam(':userState', $userState);
$stmt->bindParam(':userStateAbbrev', $userStateAbbrev);
$stmt->bindParam(':userEmail', $userEmail);
$stmt->bindParam(':userPass', $userPass);
$stmt->execute();
//check for success
if (mysql_affected_rows() > 0) {
echo "success";
} else {
echo "failed";
}
?>