문제

안녕하세요 앱 내부에서 변수를 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];

또한, 당신은 전화를 통해 조심하고 URL의 문자를 피할 수 있습니다. NSString의 방법 stringByAddingPercentEscapesUsingEncoding:, 따라서 문자열은 URL 사용을 위해 올바르게 인코딩됩니다.

다른 팁

initwithformat로 호출에서 %s의 %d를 교체하려고

NSString *urlstr = [[NSString alloc] initWithFormat:@"http://XXX URL HERE XXX/ringtone_send.php?email=%s&character=%s&charname=%s", emailField.text, picFile, charName];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top