插入远程数据库ASIFormDataRequest:获取400 Code,Bad Request

时间:2012-01-08 03:15:44

标签: php mysql web-services asiformdatarequest

我正在尝试将数据插入到Web服务器上的数据库中。我正在使用MySQL,Appache和PHP

的Xcode

*-(IBAction)saveRecord:(id)sender{
    UITextField *description_txtfield = (UITextField*)[self.tableView viewWithTag:11];
    description_string = description_txtfield.text;
    NSLog(description_string);

    NSURL *url = [NSURL URLWithString:@"http://192.168.1.140/~admin/qw/"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:description_string forKey:@"product_name"];
    [request setDelegate:self];
    [request startAsynchronous];

    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)requestFinished:(ASIHTTPRequest *)request {    

    if (request.responseStatusCode == 400) {
        NSLog(@"Invalid code");        
    } else if (request.responseStatusCode == 403) {
        NSLog(@"Code already used");
    } else if (request.responseStatusCode == 200) {
        NSLog(@"OK");

    } else {
        NSLog(@"Unexpected error");
    }
     }*

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    NSError *error = [request error];
    NSLog(error.localizedDescription);
}   

index.php里面有以下代码来处理插入。

Function redeem() {

        // Check for required parameters
        if (isset($_POST["product_name"])) {

            // Put parameters into local variables
            $rw_app_id = $_POST["product_name"];
            // Add tracking of redemption
            $stmt = $this->db->prepare("INSERT INTO inventory (product_name) VALUES (?)");
            $stmt->bind_param($rw_app_id);
            $stmt->execute();
            $stmt->close();

        }
        sendResponse(400, 'Invalid request');
        return false;

    }

有人能告诉我这里做错了什么吗?我一直收到错误的代码,这是错误的请求-400。

1 个答案:

答案 0 :(得分:0)

您在if语句中缺少标题响应和返回。否则它会掉下来并且每次都会得到400.

    Function redeem() {

            // Check for required parameters
            if (isset($_POST["product_name"])) {

                // Put parameters into local variables
                $rw_app_id = $_POST["product_name"];
                // Add tracking of redemption
                $stmt = $this->db->prepare("INSERT INTO inventory (product_name) VALUES (?)");
                $stmt->bind_param($rw_app_id);
                $stmt->execute();
                $stmt->close();
                sendResponse(200, 'Ok');
                return true;
            }
            sendResponse(400, 'Invalid request');
            return false;

        }