L'invio di variabili a PHP da Cocoa Touch su iPhone SDK
Domanda
Ciao sto cercando di passare le variabili a PHP dall'interno di un'applicazione.
<?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';
?>
E il codice app SDK:
-(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);
}
Tutto sembra funzionare, l'utente inserisce lì e-mail e colpisce inviare, la stringa di e-mail e altre 2 stringhe di dati vengono aggiunti e sembra di connettersi con il PHP, il PHP sembra funzionare durante il test direttamente da una linea di indirizzo del browser .
Si apre anche i dati in una nuova linea sul database (se una nuova email, se non aggiunge una stringa se esiste una linea di corrente per tale e-mail) in modo che tutti è grande.
E 'solo che i dati viene passato come numeri, non una stringa.
es. l'e-mail si rivolge a:
15968112
e i dati a qualcosa di simile:
70560
al posto delle stringhe originali! Cosa sta succedendo? Come può essere risolto?
Grazie in anticipo ..
Soluzione
La vostra maschera di formato per initWithFormat
è sbagliato.
Si utilizza %d
che viene utilizzato per formattare il parametro come un valore decimale, quando è necessario utilizzare %@
, per formattare il parametro come un oggetto Obj-C (come la proprietà di testo, che è un NSString
.
La linea giusta sarebbe simile a questa:
NSString *urlstr = [[NSString alloc] initWithFormat:@"http://url/ringtone_send.php?email=%@&character=%@&charname=%@",
emailField.text, picFile, charName];
Inoltre, si consiglia di fare attenzione e la fuga i caratteri per l'URL, chiamando il metodo di NSString
stringByAddingPercentEscapesUsingEncoding:
, in modo che la stringa è correttamente codificato per l'uso URL
Altri suggerimenti
cercando scambiare il% d per% s nella chiamata a initWithFormat
NSString *urlstr = [[NSString alloc] initWithFormat:@"http://XXX URL HERE XXX/ringtone_send.php?email=%s&character=%s&charname=%s", emailField.text, picFile, charName];