IOS-如何使用mysql保存数据?

时间:2016-05-19 10:44:34

标签: ios xcode7

我需要注册我的应用。我将数据保存到mysql。我尝试了一些解决方案,但它不起作用。如果有任何解决方案请帮助我知道!我通过URL将数据保存到我的sql:

这是我的功能保存数据:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    UITapGestureRecognizer *oneTapGesture = [[UITapGestureRecognizer alloc]
                                             initWithTarget: self
                                             action: @selector(hideKeyboard:)];
    [oneTapGesture setNumberOfTouchesRequired:1];
    [[self view] addGestureRecognizer:oneTapGesture];
}




      - 

(IBAction)btnSave:(id)sender {


            Pheptoan *functionStr = [[Pheptoan alloc] init];

            NSInteger success=0;

            @try{
                if([[self.txtUsername text] isEqualToString:@""] || [[self.txtPassword text] isEqualToString:@""] || [[self.txtEmail text] isEqualToString:@""]
                   || [[self.txtName text] isEqualToString:@""]|| [[self.txtTel text] isEqualToString:@""]){
                    [self showUIAlertWithMessage:@"User/Email not blank \n Password not blank" andTitle: @"Message"];
                }else{

                    NSString *password = _txtPassword.text;

                    NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];

                    NSString *passwordMD5 = [functionStr MD5:passwordData];


                    NSString *userUrl = [NSString stringWithFormat:@"http://ios.khothe.vn/web/gamecard/register/sUsername/%@/sPassword/%@/sName/%@/sEmail/%@/sTel/%@", _txtUsername.text, passwordMD5,_txtName.text,_txtEmail.text,_txtTel.text];

                    NSLog(@"link: %@", userUrl);

                    NSURL *url = [NSURL URLWithString:userUrl];

                    NSURLRequest *request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0];


                    NSError *error = nil;
                    NSHTTPURLResponse *response;
                    NSData *data= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
                    if (!data) {
                        NSLog(@"Error: %@", [error localizedDescription]);

                    }

                    if([response statusCode]>=200 && [response statusCode]<300){

                        NSError *error= nil;

                        NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

                        success = [[jsonDictionary valueForKey:@"success"] integerValue];

                        if(success == 400){
                            [self showUIAlertWithMessage:@"sucessfull" andTitle:@"Message"];

                        }else{
                            [self showUIAlertWithMessage:@"Error! Please wrong username or password" andTitle:@"Message"];
                        }

                    }

                }
            }@catch(NSException *e){
                [self showUIAlertWithMessage:@"Error! Please login again" andTitle:@"Message"];

            }

            if(success == 400){
                [self performSegueWithIdentifier:@"loginSuccess" sender:self];

            }


    }

这是我的函数在mysql中保存数据:

public function actionRegister()
    {
        $connection = Yii::app()->dbgamecard;       
        if($_GET != null){          
        $sUsername = $_GET["sUsername"];
        $sPassword = $_GET["sPassword"];
        $sName = $_GET["sName"];
        $sEmail = $_GET["sEmail"];
        $sTel = $_GET["sTel"];  

        $userInfo = Yii::app()->dbgamecard->createCommand("SELECT Username FROM member  WHERE Username=:sUsername")
                                                ->bindParam(':sUsername', $sUsername)                                                             
                                                ->queryRow();
        $arr = null;
        if($userInfo){
                    echo json_encode(array("success" => 404));
                    //$arr["Status"] = "0";
                    //$arr["Message"] = "Username Exists!";     
                    //echo json_encode($arr);
                    exit();
            }

        $userInfo = Yii::app()->dbgamecard->createCommand("SELECT Email FROM member WHERE Email=:sEmail")
                                                ->bindParam(':sEmail', $sEmail)                                                             
                                                ->queryRow();

        if($userInfo){
            echo json_encode(array("success" => 404));
                    //$arr["Status"] = "0";
                    //$arr["Message"] = "Email Exists!";        
                    //echo json_encode($arr);
                    exit();
            }           
        $strSQL = "INSERT INTO member (Username,Password,Name,Email,Tel) 
                                        VALUES(:strUsername, :strPassword, :strName, :strEmail, :strTel)";
            $command= $connection->createCommand($strSQL);
            $command->bindParam(":strUsername", $sUsername,PDO::PARAM_STR);
            $command->bindParam(":strPassword", $sPassword,PDO::PARAM_STR);
            $command->bindParam(":strName", $sName,PDO::PARAM_STR);
            $command->bindParam(":strEmail", $sEmail,PDO::PARAM_STR);
            $command->bindParam(":strTel", $sTel,PDO::PARAM_STR);   
            $command->execute();    
            if(!$command)
            {
                echo json_encode(array("success" => 404));
                //$arr["Status"] = "0";
                //$arr["Message"] = "Cannot Save Data!";                
                //echo json_encode($arr);
                exit();
            }
            else{
                 $userInfo1['success'] = 400;
                 echo json_encode($userInfo);
                //$arr["Status"] = "1";
                //$arr["Message"] = "Register Successfully!";
                //echo json_encode($arr);
                exit();
                }   
            }
        mysql_close($objConnect);
    }
}

我得错误

2016-05-19 17:17:19.738 gamecard App[1608:126645] -[RegisterController hideKeyboard:]: unrecognized selector sent to instance 0x7fdbbb645560
2016-05-19 17:17:19.742 gamecard App[1608:126645] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RegisterController hideKeyboard:]: unrecognized selector sent to instance 0x7fdbbb645560'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000103164d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010284fdeb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010316dd3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001030b3cfa ___forwarding___ + 970
    4   CoreFoundation                      0x00000001030b38a8 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x00000001045c6b28 _UIGestureRecognizerSendTargetActions + 153
    6   UIKit                               0x00000001045c319a _UIGestureRecognizerSendActions + 162
    7   UIKit                               0x00000001045c1197 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
    8   UIKit                               0x00000001045c9655 ___UIGestureRecognizerUpdate_block_invoke898 + 79
    9   UIKit                               0x00000001045c94f3 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
    10  UIKit                               0x00000001045b6e75 _UIGestureRecognizerUpdate + 2634
    11  UIKit                               0x000000010414348e -[UIWindow _sendGesturesForEvent:] + 1137
    12  UIKit                               0x00000001041446c4 -[UIWindow sendEvent:] + 849
    13  UIKit                               0x00000001040efdc6 -[UIApplication sendEvent:] + 263
    14  UIKit                               0x00000001040c9553 _UIApplicationHandleEventQueue + 6660
    15  CoreFoundation                      0x000000010308a301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x000000010308022c __CFRunLoopDoSources0 + 556
    17  CoreFoundation                      0x000000010307f6e3 __CFRunLoopRun + 867
    18  CoreFoundation                      0x000000010307f0f8 CFRunLoopRunSpecific + 488
    19  GraphicsServices                    0x0000000107ae5ad2 GSEventRunModal + 161
    20  UIKit                               0x00000001040cef09 UIApplicationMain + 171
    21  gamecard App                        0x00000001022bda9f main + 111
    22  libdyld.dylib                       0x000000010626192d start + 1
    23  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我不确定我的代码是好工作。但是如果有关于将数据保存到mysql的任何解决方案,请告诉我。我感谢所有的支持。谢谢你!

1 个答案:

答案 0 :(得分:0)

错误是因为您调用了一个不存在的方法“hideKeyBoard”。

错误不属于保存数据的方法或什么。