繁体   English   中英

php在mysql中将数据存储为Optional(“ a”)而不是普通字符串

[英]php storing data in mysql as Optional("a") and not a normal String

我的swift应用程序中有一个问题,每当我注册一个新用户以登录到我的系统时,该字符串就会被解析为不同的内容并存储用户名,例如, 参见数据输入

我不知道为什么要这么做。 这是我在xcode swift中的代码:

    let myURL = NSURL(string: "http://localhost:8888/userRegister.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 {
        print("error=\(error)")
        return
        }


        do{

        let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
            if let parseJSON = json{
             let resultValue = parseJSON["status"] as! String
                print("result:\(resultValue)")
                var isUserRegistered: Bool = false;

                if(resultValue == "success"){
                    isUserRegistered = true;
                }
                var messageToDisplay:String = parseJSON["message"] as! String;
                if (!isUserRegistered)
                {
                    messageToDisplay = parseJSON["message"] as! String;
                }

                dispatch_async(dispatch_get_main_queue(), {
                    let 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);
            }


                )};


        } catch { print(error)}
    }




    task.resume();

我的php文件(userRegister.php)

<?php 

require("Conn.php");
require("MySQLDao.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 MySQLDao();
$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();

?>

最后是存储查询的mysqlDao.php文件

<?php
class MySQLDao {
var $dbhost = null;
var $dbuser = null;
var $dbpass = null;
var $conn = null;
var $dbname = null;
var $result = null;

function __construct() {
$this->dbhost = Conn::$dbhost;
$this->dbuser = Conn::$dbuser;
$this->dbpass = Conn::$dbpass;
$this->dbname = Conn::$dbname;
}

public function openConnection() {
$this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this-    >dbname);
if (mysqli_connect_errno())
echo new Exception("Could not establish connection with database");
}

public function getConnection() {
return $this->conn;
}

public function closeConnection() {
if ($this->conn != null)
$this->conn->close();
}

public function getUserDetails($email)
{
$returnValue = array();
$sql = "select * from users where username='" . $email . "'";

$result = $this->conn->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$returnValue = $row;
}
}
return $returnValue;
}

public function getUserDetailsWithPassword($email, $userPassword)
{
$returnValue = array();
$sql = "select id,username from users where username='" . $email . "' and      password='" .$userPassword . "'";

$result = $this->conn->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$returnValue = $row;
}
}
return $returnValue;
}

public function registerUser($email, $password)
{
$sql = "insert into users set username=?, password=?";
$statement = $this->conn->prepare($sql);

if (!$statement)
throw new Exception($statement->error);

$statement->bind_param(ss, $email, $password);
$returnValue = $statement->execute();

return $returnValue;
}

}
?>

谢谢

这是我之前和之后的工作代码。 只需解开可选变量即可。

之前:

let postString = "email=\(email)&password=\(password)&phone=\    (phone)&name=\(name)"

例如,名称的存储方式如下: Optional("Wissa")

后:

let postString = "email=\(email!)&password=\(password!)&phone=\    (phone!)&name=\(name!)"

这样对我来说效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM