views:

662

answers:

2

Hi am trying to pass variables to php from inside an app.

<?php
// get email address
$email = $_GET['email'];
// get character name
$character = $_GET['character'];
// get password
$charname = $_GET['charname'];
// set up variables
$id = 0;
$idtest="no";
// set up database connection
$link = mysql_connect("localhost", "xxx username xx","xxx password xxx") or die ("Unable to connect to database.");
mysql_select_db("xxx db xxx") or die ("Unable to select database.");
// contact and check if an email already exists
$sqlstatement= "SELECT id, ringtones FROM users WHERE email = '$email'";
$newquery = mysql_query($sqlstatement, $link);
while (list($id, $ringtones) = mysql_fetch_row($newquery)) {
    echo"&id=$id&ringtones=$ringtones";
    $ringtoneString=$ringtones;
    if ($id <> 0) { 
     $idtest="yes";
    }
}
// if idtest flag = no then add new user
if ($idtest == "no") {
    // add a space to the end of character and store in new string
    $character2 = $character . ' ';
    $sqlstatement= "INSERT INTO users (email, ringtones) VALUES ('$email', '$character2')";
    $newquery = mysql_query($sqlstatement, $link);
    echo ' registered new email address '; 
    echo $email;
// else update the ringtone field
} else {
    //$sqlstatement= "INSERT INTO users (email) VALUES ('$email') WHERE email = '$email'";
    //$newquery = mysql_query($sqlstatement, $link);
    echo ' Updated email address'; 
    echo $email;
    echo ' current ringtones = '; 
    echo $ringtoneString;
    // add new character to ringtone string
    $ringtoneString = $ringtoneString . $character . ' ';
    echo ' updated ringtone string = '; 
    echo $ringtoneString;
    // add new rintone string back in to user
    $query = "UPDATE users SET ringtones = '$ringtoneString' WHERE email = '$email'";
    $success=mysql_query($query);
    if ($success) echo "The insert query was successful. '$loadconnect'";
     else echo "Error: insert query failed. '$loadfailed'";
}
// email with attachment
// turn character 3 into a proper name
$character3 = $character;


// email with attachment script goes here

//create short variable names
$fromname = 'FROM';
$fromemail='FROM EMAIL';
$subject="SUBJECT";
$message="MESSAGE HERE";

$email=trim($email);
$subject=StripSlashes($subject);
$message=StripSlashes($message);

    mail($email,$subject,$message,"From: FROM EMAIL");
     //clear the variables
     $name='';
     $email='';
     $subject='';
     $message='';
     echo 'response=passed';


?>

And app SDK code:

-(IBAction)sendEmail:(id)sender{
    //NSpicFile = picFile;
    //NScharName = charName;
    //NSemail = emailField.text;
    NSString *urlstr = [[NSString alloc] initWithFormat:@"http://XXX URL HERE XXX/ringtone_send.php?email=%d&character=%d&charname=%d", emailField.text, picFile, charName];
    NSURL *url = [[NSURL alloc] initWithString:urlstr];
    NSString *ans = [NSString stringWithContentsOfURL:url];
    // here in ans you'll have what the PHP side returned. Do whatever you want
    [urlstr release];
    [url release];

    NSLog(@"email sent to = %@", emailField.text);
    NSLog(@"data sent = %@ - %@", picFile, charName);
    NSLog(@"url string = %d", urlstr);


}

It all seems to work, the user enters there email and hits send, the email string and 2 other data strings are added and it seems to connect with the PHP, the PHP seems to work when tested directly from a browser address line.

It even pops the data into a new line on the database (if a new email, if not appends a string if a current line exists for that email) so all is great.

It's just that the data gets passed as numbers, not a string.

e.g. the email turns to:

15968112

and the data to something like:

70560

instead of the original strings! What is going on? How can this be remedied?

Thanks in advance..

+1  A: 

trying swapping the %d for %s in your call to initWithFormat

NSString *urlstr = [[NSString alloc] initWithFormat:@"http://XXX URL HERE XXX/ringtone_send.php?email=%s&character=%s&charname=%s", emailField.text, picFile, charName];
iAn
thanks, believe this may work as well
Joe
Not bad for a guess ;)
iAn
+3  A: 

Your format mask for initWithFormat is wrong.

You are using %d which is used to format the parameter as a decimal value, when you need to use %@, to format the parameter as an Obj-C object (like the text property, that is a NSString.

The correct line would look like this:

    NSString *urlstr = [[NSString alloc] initWithFormat:@"http://url/ringtone_send.php?email=%@&amp;character=%@&amp;charname=%@",
       emailField.text, picFile, charName];

Also, you may want to be careful and escape the characters for URL, by calling NSString's method stringByAddingPercentEscapesUsingEncoding:, so the string is properly encoded for URL use.

pgb
brilliant, works a treat with bonus tip as well. Thanks
Joe