使用ios中的POST请求在服务器数据库中重复记录插入

时间:2013-02-16 13:31:24

标签: iphone ios6 ios5

-(void) registerintoserverDB{
NSString *str_registerURL = @"reg_action.php";

NSString *str_completeURL = [NSString stringWithFormat:@"%@%@", str_global_domain, str_registerURL]; 

NSURL *url = [NSURL URLWithString:str_completeURL];  

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

NSString *str_address = [NSString stringWithFormat:@"%@%@", txt_address1.text, txt_address2.text];   

NSString *postData = [NSString stringWithFormat:@"rest_name=%@&rest_telephone=%@&rest_email=%@&rest_password=%@&rest_country=%@&rest_city=%@&rest_postal=%@&rest_delivery=%@&rest_address=%@&rest_image=%@&rest_minimum_order=%@&isUser=%@&fb_name=%@fb_like", txt_restaurantname.text, txt_telephone.text, txt_email.text, txt_pass.text, txt_country.text, txt_city.text, txt_postal.text, txt_deliveryHours.text,  str_address, @"imagepath", @"10 euro", @"Yes", str_global_user_fbUsername];  

NSString *length = [NSString stringWithFormat:@"%d", [postData length]];
[theRequest setValue:length forHTTPHeaderField:@"Content-Length"]; 

[theRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];  

NSURLConnection *sConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
[sConnection start];   

NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
NSLog(@"response data is %@", responseData);  

NSString *returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog(@"returnString....%@",returnString);
NSDictionary *response_dic = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];

NSLog(@"msg is  : %@ ",[response_dic objectForKey:@"msg"]);}

我想将用户详细信息插入到服务器端数据库中,所以我使用这段代码它工作正常,但问题是在我的服务器数据库中重复条目被插入像2条记录具有相同的细节无法识别在哪里问题正好在我的代码端或服务器端。请尽快提出一些指导原则。提前谢谢!

1 个答案:

答案 0 :(得分:4)

删除:

NSURLConnection *sConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
[sConnection start];   

或者这个:

NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];

即使你有一个请求,你也会建立两个不同的连接(其中一个是异步连接,另一个是同步)。两者都相等,两个插入数据库。