php - 从 iPhone sdk 上的 Cocoa Touch 发送变量到 php

标签 php cocoa string url encode

您好,我正在尝试从应用程序内部将变量传递给 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

代替原来的字符串!到底是怎么回事?如何解决这个问题?

提前致谢..

最佳答案

您的 initWithFormat 格式掩码错误。

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

正确的行应如下所示:

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

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

关于php - 从 iPhone sdk 上的 Cocoa Touch 发送变量到 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1673228/

相关文章:

macos - NSTableView -noteNumberOfRowsChanged 的​​ NSView 版本是什么?

objective-c - 不使用 XCode 进行 Iphone 开发

ruby - 如何找到字符串的最长重复序列

java - Java内部如何表示字符串?

php - 列出父子树结构

php - Prestashop 1.6.0.9 - 循环中的 $product->save() 只需要 10 个产品

php - PHP MySQL 查询中 URL 的变量

php - 严格标准 : Only variables should be passed by reference PHP Shopping Cart

cocoa - 如何以一定角度绘制 NSString?

c - strcpy函数: C