php - 为什么我的 Swift 应用程序无法连接到我的数据库?

标签 php ios mysql xcode swift

这是当用户按下注册页面上的“注册”按钮时我正在执行的代码。我只想让脚本将信息发送到我的 Register.php 页面,然后将电子邮件和密码放入数据库。

@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)
            });
        }
    }
}

注册.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();

?>'

最佳答案

您可能在 mysql 中输入的变量名称与 php db 文件中的变量名称不正确。它们区分大小写。检查以确保 mysql“user”表 php 脚本中的列名称没有空格。希望有帮助

关于php - 为什么我的 Swift 应用程序无法连接到我的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30103069/

相关文章:

ios - 如何创建 SKViaPoint 的实例

php - Yii2 MySQL 服务器已经消失

php - WordPress - 导出特色图像 mysql

php - 为什么我的数据不会进入我的数据库?

php - 如何在文件中传递超过 9 个参数。 htaccess(mod_rewrite)?

php - 如何生成 Y-m-d 格式的工作日数组?

iphone - 保证 iPhone 应用程序的 URL 方案 (CFBundleURLSchemes) 条目的唯一性

php - jQuery 中的空格破坏了 img src

ios - 如何从 API 调用中获取原始 JSON

MySql 通过查询将两个日期列合并为一列