Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php将数据存储在mysql中作为可选(“a”)而不是普通字符串_Php_Mysql_Json_Swift - Fatal编程技术网

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

php将数据存储在mysql中作为可选(“a”)而不是普通字符串,php,mysql,json,swift,Php,Mysql,Json,Swift,我的swift应用程序有一个问题,每当我注册一个新用户登录到我的系统时,字符串就会被解析成不同的内容,并存储用户名,例如 我不知道它为什么这样做。以下是我在xcode swift中的代码: let myURL = NSURL(string: "http://localhost:8888/userRegister.php"); let request = NSMutableURLRequest(URL: myURL!); request.HTTPMethod = "PO

我的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)


最后是存储查询的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)"
例如,名称存储如下:
可选(“Wissa”)

之后:

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

这种方式对我很有效。

看起来您的电子邮件和密码字段是可选的,您应该在发布前将它们插入字符串时打开它们。
$\u POST[“email”]
输出为什么?为什么要将电子邮件和密码实体化(密码应该散列)?我不认为我的字段是可选的。您能否指出,PBUSH25看起来像是您将
可选(“a”)
作为一个值发布到PHP中,因此该错误最有可能出现在您的swift应用程序中htmlentities
替换为
,这就是为什么您会得到
可选(“a”)
。您可能还想看看?@user3423164我不知道您的电子邮件和密码字段声明在哪里,但因为您将它们放入字符串
“\(emailField)和一些比\(passwordField)“
更多的文本中,而这些变量被错误地放入了您的数据库中,我的第一个想法是它们可能是可选的。这里没有足够的代码让我可以确定,这就是为什么我建议使用它。
let postString = "email=\(email!)&password=\(password!)&phone=\    (phone!)&name=\(name!)"