$_POST到SQL更新问题的问题


$_POST to SQL update problems problems

我正试图从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

您的查询缺少变量$header

修正查询:

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

您没有在查询中包括您的变量$header,这就是为什么$header中的值不用于更新数据库中的列

改变这一行:

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

:

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