gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:27:50 25 4
gpt4 key购买 nike

这是当用户按下注册页面上的“注册”按钮时我正在执行的代码。我只想让脚本将信息发送到我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com