2

您好我正在尝试从应用程序内部将变量传递给 php。

<?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';


?>

和应用 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);


}

一切似乎都正常,用户在那里输入电子邮件并点击发送,添加了电子邮件字符串和 2 个其他数据字符串,它似乎与 PHP 连接,当直接从浏览器地址行测试时,PHP 似乎工作。

它甚至将数据弹出到数据库的新行中(如果是新电子邮件,如果没有,则如果该电子邮件存在当前行,则附加一个字符串),所以一切都很好。

只是数据作为数字而不是字符串传递。

例如,电子邮件变成:

15968112

和数据类似:

70560

而不是原来的字符串!到底是怎么回事?如何补救?

提前致谢..

4

2 回答 2

4

您的格式掩码initWithFormat错误。

您正在使用%dwhich 用于将参数格式化为十进制值,当您需要使用%@, 将参数格式化为 Obj-C 对象(如 text 属性,即NSString.

正确的行如下所示:

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

此外,您可能需要小心并通过调用NSString's 方法来转义 URL 的字符stringByAddingPercentEscapesUsingEncoding:,以便正确编码字符串以供 URL 使用。

于 2009-11-04T12:24:15.890 回答
1

尝试在对 initWithFormat 的调用中将 %d 换成 %s

NSString *urlstr = [[NSString alloc] initWithFormat:@"http://XXX URL HERE XXX/ringtone_send.php?email=%s&character=%s&charname=%s", emailField.text, picFile, charName];
于 2009-11-04T11:36:44.583 回答