UPDATE数据没有进入php文件?

时间:2016-01-15 15:24:10

标签: php ios objective-c updates

当我尝试从iOS到PHP服务器的UPDATE / PUT字段时,我没有获得这些值。

这是我的iOS代码

NSString *url=@"http://example.com/project/php/basicinfo.php";


    NSString *stringData = [NSString stringWithFormat:@"fname=%@&mname=%@&lname=%@&email=%@&country=%@&city=%@&dob=%@&role=%@",firstNameCell.fname.text ,mnameCell.mname.text,lnameCell.lname.text,emailCell.email.text,[countryCell.country currentTitle],cityCell.city.text,[dobCell.dob currentTitle],@"Model"];
    NSLog(@"%@",stringData);
    NSURL *aUrl = [NSURL URLWithString:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request setHTTPMethod:@"UPDATE"];
    [request setHTTPBody:[stringData dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];

当我在PHP中echo时它变为空。

请帮助我

这是接收回复

 Did Receive Response <NSHTTPURLResponse: 0x14ef17e40> { URL: http://example.com/project/php/basicinfo.php } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Length" = 1;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Fri, 15 Jan 2016 15:15:40 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.4.7 (Ubuntu)";
    "X-Powered-By" = "PHP/5.5.9-1ubuntu4.11";
} }

PHP代码

<?php
header("Content-type: text/html; charset=utf-8"); 
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$charset="UTF8";
$fname=$_POST['fname'];
$mname=$_POST['mname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$country=$_POST['country'];
$city=$_POST['city'];
$dob=$_POST['dob'];
$role=$_POST['role'];
   echo $name;

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

/* change character set to utf8 */
if (!mysqli_set_charset($conn, "utf8")) {
    exit();
} else {
    mysqli_close($link);

   echo $email;
    $sql="UPDATE `basicinfo` SET `fname`='$fname',`mname`='$mname',`lname`='$lname',`email`='$email',`country`='$country',`city`='$city',`dob`='$dob',`role`='$role' WHERE `email`='$email'";

if ($conn->query($sql) === TRUE) {
  //  echo $email;
} else {
    echo "fail";
}
    //printf("Current character set: %s\n", mysqli_character_set_name($conn));
}

?>

1 个答案:

答案 0 :(得分:0)

如果你使用UPDATE HTTP方法来调用你的脚本,$ _POST将无法正常工作。

你可以这样做:

 $rest = $_SERVER['REQUEST_METHOD'];
    if(isset($_SERVER['CONTENT_TYPE'])) {
        $content_type = $_SERVER['CONTENT_TYPE'];
    }

if ($rest=="PUT") {

$body = file_get_contents("php://input");
            parse_str($body, $parsed);
            switch ($content_type) 
            {
                case "application/json":
                   // do stuff
                    break;
                case "application/x-www-form-urlencoded":
                    // do stuff
                     break;
            }
            break;
        }


}

或使用POST