3

I am really struggling trying to understand why the below code does not insert the date I am sending?

<?php
$servername = "servername";
$username = "username";
$password = "passwrd";
$dbname = "dbname";


$conn  = new PDO ("mysql:host=$servername;dbname=$dbname", $username, $password); 
$conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$Name = $_POST['Name'];
$Last = $_POST['Last'];
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$userID = $_POST['userID'];

$sql = "INSERT INTO Users (Name, Last, email, password, userID) VALUES (:var1, :var2, :var3, :var4, :var5) "; 

$q = $conn -> prepare($sql);

$q -> execute (array(':var1' => $Name,
               ':var2' => $Last,
               ':var3' => $email,
               ':var4' => $pwd,
               ':var5' => $userID));

?>

If instead of

$Name = $_POST['Name']; 

and so on ... I hard code the values, so I might have:

$Name = 'Jane';

It adds the data to the database, but with $_POST, it fails at the execute line.

My Xcode looks like:

NSString *strURL = [NSString stringWithFormat:@"http://www.myserver/AddUser.php?Name=%@&Last=%@&email=%@&pwd=%@&userID=%@", Name, Last, email, pwd, userID];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

I have gone through numerous tutorials and questions in SO and I can't see what the problem is.

5
  • 1
    I don't know anything about Xcode, but looks like a GET request to me. If that's the default and would explain why $_POST is empty. Commented Jun 3, 2015 at 15:38
  • @AbraCadaver what do you mean? Put GET instead of POST? I am sending data from my iPhone app to a sql server ... Commented Jun 3, 2015 at 15:39
  • @user3079872 print_r($_POST) to see if you have any values or not. Commented Jun 3, 2015 at 15:45
  • @AbraCadaver, you are absolutely correct, it works with GET instead of POST. Commented Jun 3, 2015 at 15:51
  • @Alex, yes there were no values, but I could not work out why. Commented Jun 3, 2015 at 15:51

2 Answers 2

3

Your HTTP request is being sent using the GET protocol, but you are accessing your data using the POST protocol. You have three potential fixes,

  1. Change PHP to read data using GET

    $email = $_GET['email'];

  2. Change PHP to be agnostic to mode (my preference)

    $email = $_REQUEST['email'];

  3. Change your iPhone app to send using post.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks very much for the explanation. I have now put $_REQUEST and it is inserting the data into the database. Out of interest, how would I change the app to send using POST?
You must configure the NSMutableURLRequest object you provide to the initWithRequest:delegate: method and construct the body data. NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setHTTPMethod:@"POST"];
thanks very much, makes a lot of sense! Thanks again for your help.
3

It seems as though you're issuing a get request, not a post request, which means your $_POST is undefined, which is probably what's causing PDO's errors you're seeing. Instead, you should retrieve the values from the $_GET global variable:

$Name = $_GET['Name'];
$Last = $_GET['Last'];
$email = $_GET['email'];
$pwd = $_GET['pwd'];
$userID = $_GET['userID'];

1 Comment

thanks very much. I have tried $_GET and it works. I have gone with the $_REQUEST rather than $_GET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.