2015-05-07 90 views
0

這是我在用戶按下注冊頁面上的「註冊」按鈕時執行的代碼。我只希望腳本將信息發送到我的Register.php頁面,然後將電子郵件和密碼放入數據庫。爲什麼我的Swift應用程序沒有連接到我的數據庫?

@IBAction func RegisterButtonTapped(sender: AnyObject) { 

    let userEmail = EmailTextField.text 
    let userPassword = PasswordTextField.text 
    let userRepeatPassword = RepeatPasswordTextField.text 

    // Check for empty fields 
    if(userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty) { 

     // Display alert message 

     displayAlertMessage("All fields are required.") 

     return; 
    } 

    // Check if passwords match 
    if(userPassword != userRepeatPassword) { 
     // Display an alert message 
     userPassword == "" 
     displayAlertMessage("Passwords do not match.") 
     return; 
    } 

    // Send user data to server side 
    let myUrl = NSURL(string: "www.testsite.com/Register.php") 
    let request = NSMutableURLRequest(URL: myUrl!) 

    request.HTTPMethod = "POST"; 

    let postString = "email=\(userEmail)&password=\(userPassword)" 

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 
     data, response, error in 

     if error != nil { 
      println("error\(error)") 
      return 
     } 

     var err: NSError? 
     var json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error:&err) as? NSDictionary 

     if let parseJSON = json { 
      var resultValue = parseJSON["status"] as? String 
      println("result: \(resultValue)") 

      var isUserRegistered:Bool = false 
      if (resultValue=="Success") { 
       isUserRegistered = true 
      } 

      var messageToDisplay = parseJSON["message"] as! String! 
      if (!isUserRegistered) { 
       messageToDisplay = parseJSON["message"] as! String! 
      } 

      dispatch_async(dispatch_get_main_queue(), { 

       //Display alert message with confirmation 
       var myAlert = UIAlertController(title: "Alert", message:messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert) 

       let okAction = UIAlertAction(title: "Ok", style:UIAlertActionStyle.Default){ action in 
        self.dismissViewControllerAnimated(true, completion: nil) 
       } 

       myAlert.addAction(okAction) 
       self.presentViewController(myAlert, animated: true, completion: nil) 
      }); 
     } 
    } 
} 

Register.php

<?php 
require(「Connect.php」); 
require(「SQLData.php」); 
$email = htmlentities($_POST[「email」]); 
$password = htmlentities($_POST[「password」]); 

$returnValue = array(); 

if(empty($email) || empty($password)) { 
    $returnValue[「status」] = 「error」; 
    $returnValue[「message」] = 「Missing required field」; 
    echo json_encode($returnValue); 
    return; 
} 

$dao = new SQLData(); 
$dao->openConnection(); 
$userDetails = $dao->getUserDetails($email); 

if(!empty($userDetails)) { 
    $returnValue[「status」] = 「error」; 
    $returnValue[「message」] = 「User already exists」; 
    echo json_encode($returnValue); 
    return; 
} 

$secure_password = md5($password); // I do this, so that user password cannot be read even by me 

$result = $dao->registerUser($email,$secure_password); 

if($result) { 
    $returnValue[「status」] = 「Success」; 
    $returnValue[「message」] = 「User is registered」; 
    echo json_encode($returnValue); 
    return; 
} 

$dao->closeConnection(); 

?>' 
+1

你的問題是什麼?微不足道:如果你真的關心用戶的密碼。你真的做了一些事情(真的)是錯誤的。 – milo526

+0

你能看到我要去哪裏嗎?它不是填充數據庫,有無論如何測試它是否被髮送到PHP頁面? – FreeTheStudents

+0

所以你的問題是:爲什麼我的Swift應用程序沒有連接到我的數據庫?請編輯您的問題以包含此內容。 – milo526

回答

0

這很可能是你在mysql中輸入的變量名稱不正確的VS PHP數據庫文件。它們區分大小寫。請檢查以確保mysql「user」表php腳本中的列名稱不含空格。希望有幫助

相關問題