1
-(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つのレコードのように重複したエントリが挿入され、問題がどこにあるかを正確に認識できません。私のコード側またはサーバー側で。できるだけ早くいくつかのガイドラインを提案してください。前もって感謝します!

4

1 に答える 1

4

次のいずれかを削除します。

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

またはこれ:

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

リクエストが1つある場合でも、2つの異なる接続を確立しています(1つは非同期接続を実行し、もう1つは同期接続を実行しています)。両方とも、データベースへの2つの挿入に相当します。

于 2013-02-16T13:34:18.963 に答える