2016-01-15 33 views
0

當我試圖更新/ PUT字段從iOS到PHP服務器,並沒有得到這些值。更新數據沒有得到在PHP文件?

這是我的iOS代碼

NSString *[email protected]"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)); 
} 

?> 
+0

顯示你的PHP代碼... –

+0

@MarcBplease檢查更新的代碼 – Bangalore

+0

$ _ POST PHP看到POST動詞時,只填充。你正在做一個HTTP更新,所以$ _POST將是空的。甚至不知道如何獲得PHP中的'UPDATE'數據。也許從'php:// input'生讀它? –

回答

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