$ _POST到SQL更新问题

时间:2015-07-27 09:00:02

标签: php ios mysql post phpmyadmin

我正在尝试从PHP脚本更新SQL表,该脚本从我正在尝试开发的iPhone应用程序获取其POST语句。应用程序运行良好,连接在那里,但我感觉PHP脚本没有拿起我从应用程序发送的内容(因为我的数据库没有更新)。我正在使用标准应用程序 - PHP - 数据库设置。

有人可以从应用程序端查看我的PHP +帖子声明吗?我有这就是它破碎的地方。如果我运行PHP脚本,我的echo语句(如下)是空的。我发布了以下所有代码。

PHP方

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
    $header = $_POST['header'];
//    $description = $_POST['description'];
//    $city = $_POST['city'];

  $sql = "UPDATE CityTable SET head=header WHERE id=1";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
} 
//    $para = $_POST['parameter'];
//    $parad = $_POST['parameterData'];
    var_dump($_POST);
    echo '<br>', '<br>';

    echo "Header, description and city:" , '<br>', '<br>';

    echo "Header : " .$header, '<br>';
    echo "Desc : " .$description, '<br>';
    echo "City : ".$city, '<br>','<br>';

//    echo "Para : " .$para, '<br>';
//    echo "Parad : ".$parad, '<br>';
$conn->close();

?>

我的viewcontroller

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController{
    NSMutableData *mutableData;

#define URL            @"http://mydb.com/upload.php"  // change this URL
#define NO_CONNECTION  @"No Connection"
#define NO_VALUES      @"Please enter parameter values"

}

- (void)viewDidLoad {

    [super viewDidLoad];

}

-(IBAction)sendDataUsingPost:(id)sender{

    [self sendDataToServer :@"POST"];

}

-(void) sendDataToServer : (NSString *) method{

    NSString *head  = header.text;
    NSString *desc = description.text;
    NSString *cty  = city.text;

    if(head.length > 0 && desc.length > 0 && cty.length > 0){

        serverResponse.text = @"Sending to server...";

        NSURL *url = nil;
        NSMutableURLRequest *request = nil;

        // Only Difference between POST and GET is only in the way they send parameters

        if([method isEqualToString:@"POST"]){

            NSString *parameter = [NSString stringWithFormat:@"header=%@&description=%@&city=%@", head, desc, cty ];
            NSData *parameterData = [parameter dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
            url = [NSURL URLWithString: URL];
            request = [NSMutableURLRequest requestWithURL:url];
            [request setHTTPBody:parameterData];

        }

        [request setHTTPMethod:@"POST"];
        [request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

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

        if(connection)
        {

            mutableData = [NSMutableData new];

        }else{

            serverResponse.text = NO_CONNECTION;

        }

    }else{

        serverResponse.text = NO_VALUES;

    }

}

#pragma mark NSURLConnection delegates

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    [mutableData setLength:0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mutableData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    serverResponse.text = NO_CONNECTION;
    return;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *responseStringWithEncoded = [[NSString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
    //NSLog(@"Response from Server : %@", responseStringWithEncoded);
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    serverResponse.attributedText = attrStr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

2 个答案:

答案 0 :(得分:1)

您的查询缺少变量$header

更正了查询:

$sql = "UPDATE CityTable SET head='{$header}' WHERE id=1";

答案 1 :(得分:0)

您没有在查询中包含变量$header,这就是为什么$sql = "UPDATE CityTable SET head=header WHERE id=1"; 中的值未用于更新列 head 在您的数据库中。

更改此行:

$sql = "UPDATE CityTable SET head= '".$header."' WHERE id= 1";

为:

$_SESSION