1

我已经通过我的 MAC 上的 MAMP 建立了一个服务器。有人在下面编写了 PHP 脚本:

header("Content-Type:text/html; charset=utf-8");
if(!isset($_COOKIE['hostname']))$hostname = "http://127.0.0.1";
    else $hostname = $_COOKIE['hostname'];
if(!isset($_COOKIE['username']))$username = "root";
    else $username = $_COOKIE['username'];
if(!isset($_COOKIE['password']))$password = "";
    else $password = $_COOKIE['password'];

if(!isset($_POST['operate'])){
    echo "Please Input:";
    print(json_encode("Error:Please Input:"));
    return false;
}
else{
    $operate = $_POST['operate'];
    switch ($operate){
        case "login":
            login($_POST['hostname'],$_POST['username'],$_POST['password']);
            break;
        case "clean":
            clean();
            break;
        case "search":
            search($_POST['form'], $_POST['field'], $_POST['value']);
            break;
        case "read":
            if(!isset($_POST['num']))read1($_POST['form']);
            else read2($_POST['$form'], $_POST['$base'], $_POST['$num']);
            break;
        case "alter":
            alter($_POST['form'], $_POST['field'], $_POST['id'],$_POST['value']);
            break;
        case "delete":
            delete($_POST['form'],$_POST['id']);
            break;
        case "insert":
            if(isset($_POST['device']))insert1($_POST['form'],$_POST['device'], $_POST['port'], $_POST['vlan'], $_POST['singleMulti'], $_POST['function'], $_POST['destDevice']);
            else insert2();
            break;
        default:
            echo "Please Input:";
            print(json_encode("Error:Please Input:"));
            return false;

    }
}

function login($hostname,$username,$password){
//Login
    $link = @mysql_connect($hostname,$username,$password);
    if(!$link){
        echo "Fail to connect the DataBase";
        print(json_encode("Error:Fail to connect the DataBase"));
        return false;
    }
    mysql_query("set names utf8");
    mysql_select_db("qrcode");

    setcookie("hostname", $hostname, time()+ 9999999);
    setcookie("username", $username, time()+ 9999999);
    setcookie("password", $password, time()+ 9999999);
    mysql_close();
    return true;
}
function search($form, $field, $value){
//query
    login($hostname,$username,$password);
    $sql="select * from $form where $field = '$value'";
    $result = @mysql_result($sql);

    if(!$result){
        echo "Fail to query the DataBase";
        print(json_encode("Error:Fail to query the DataBase"));
        return false;

    }
    while($e=mysql_fetch_assoc($result))
        $output[] =$e;  
    echo json_encode($output);  
    mysql_close();
    return true;
}

function read1($form){
    if(read2($form, 0, 5))return true;      
    else return false;
}

    //Clean ...............
//Load.................
    //Alter................
//Delete...............
//Insert...............

而我使用ASIHTTPRequest,代码如下:

NSURL *url=[NSURL URLWithString:@"http://localhost/QRcode.php"];
ASIFormDataRequest *request=[[ASIFormDataRequest alloc]initWithURL:url];

[request setRequestMethod:@"POST"];
[request setDelegate:self];

[request setPostValue:@"login" forKey:@"operate"];
[request setPostValue:@"localhost" forKey:@"hostname"];
[request setPostValue:@"root" forKey:@"username"];
[request setPostValue:@"root" forKey:@"password"];

[request setPostValue:@"search" forKey:@"operate"];
[request setPostValue:@"5020" forKey:@"form"];
[request setPostValue:@"id" forKey:@"field"];
[request setPostValue:@"1" forKey:@"value"];

[request startSynchronous];
NSLog(@"%@",[request responseString]);

我很确定“5020”表格已经存在于我的数据库中,但我得到了这个结果:

Fail to query the DataBase Error:\u6570\u636e\u5e93\u67e5\u8be2\u5931\u8d25"

我尝试搜索其他表单,但仍然显示相同的错误。

谢谢你。

4

1 回答 1

0

解决了.....类似:的字符串u\6570\u5647\u......是UTF-8编码,您只需将应用程序的语言设置更改为中文,然后所有奇怪的字符串都消失了.....

于 2013-12-05T02:18:01.950 回答