从Swift检索数据到PHP然后回到Swift [重复]

In my PHP file, I have this code:

$user_id = $_POST['user_id']; 
require_once('Connect.php'); 

$sql = "SELECT * FROM Loans WHERE user_id='$user_id'"; 

I check it in HTML and it works. Nothing's wrong with it. However, when I try to put it in Swift, I have a problem with that. I don't how will I put value of "user_id" from Swift file to PHP file then go back to Swift to execute the PHP file.

Here's my Swift code:

if let loanArray = jsonObj!.value(forKey: "loans")  as? NSArray
                        {
                            for loans in loanArray
                            {
                                if let loanDict = loans as? NSDictionary
                                {
                                    if let name = loanDict.value(forKey: getBnum)
                                    {
                                        self.borrNoArray.append(name as! String)
                                    }

                                    if let name = loanDict.value(forKey: "loan_no")
                                    {
                                        self.loanNoArray.append(name as! String)
                                    }

                                    if let name = loanDict.value(forKey: "amount")
                                    {
                                        self.loanAmtArray.append(name as! String)
                                    }

                                    OperationQueue.main.addOperation({
                                        self.tableView.reloadData()
                                    })
                                }
                            }
                        }

I already tried this Swift code and it also works. I have a default value for "user_id", it is 0 and I just add some info about it just for me to see if my Swift code works and it is.

</div>
duanmaifu3428
duanmaifu3428 这看起来像是原始问题的第二个副本,Jan?
3 年多之前 回复

1个回答

Swift 3.1

In PHP

By your requirement there are different options available. As well as above code the same way on connect.php. But you need to write SELECT query to INSERT query for send parameters through mobile end and Also you need one more reference variable to get whether the method is INSERT or SELECT

For example,

Just consider you are get value from URL like https://somesite.somedomain/databaseoperations.php and your databaseoperations.php has following queries

$user_id = $_POST['user_id']; 
$operation = $_POST['operation']; 
require_once('Connect.php'); 

if($operation == "select"){
$sql = "SELECT * FROM Loans WHERE user_id=?"; 
....
// Here, perform Select from database using $user_id as a
// bound param, then echo json_encode()
}else if ($operation == "insert"){
 //Just perform Database Insert operation
}else{
 echo json_encode(["error"=>"Illegal Operation"]);
}

In iOS

Just perform URLRequest with post parameter with user_id and operation.

For get Value your post parameters like below,

let url = "https://somsite.somedomain/databaseoperations.php"
let postParam = "user_id=4&operation=select"

For Put Value postParam becomes,

let postParam = "user_id=5&operation=insert"

For reference :-

let postParam="user_id=5&operation=insert"
let url = URL(string: "https://somsite.somedomain/databaseoperations.php")
var request = URLRequest(url: url!)
request.httpBody = postParam.data(using: String.Encoding.utf8)
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request) { data,response,error in
        guard error == nil else{
            print(error!)
            return
        }

    do{
        let jsonObject = try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
        print(jsonObject)
    }catch{

        print(error.localizedDescription)
    }
}
task.resume()

I hope it'll be give some idea to you.

doulangchao8934
doulangchao8934 我在上面提到的这个问题的代码是在“func showTable()”中,我将它调用到我的覆盖func viewDidLoad(),以便我可以看到输出。 但是,考虑到你给我的代码,我将把“showTable()”放在哪里? 对不起,我很抱歉。 这个编程语言我只有2个月大了。
3 年多之前 回复
douyeyan0650
douyeyan0650 好的,非常感谢你兄弟。 我介意并会保留:)
3 年多之前 回复
doubeizhong5178
doubeizhong5178 我已经使代码更安全,并提到了绑定参数,因此初学者不会复制不安全的示例。 请不要养成这样的习惯,因为这不是一个明确的安全问题,你不必教好安全实践。 初学者将复制您提供的内容,无论是否有好处。
3 年多之前 回复
dongtanzhu5417
dongtanzhu5417 您的databaseoperations.php中有一个SQL注入漏洞。 您是否也向OP建议如何限制对数据的访问,以便有人使用Curl不能简单地下载整个表? 我假设一个名为Loans的表包含私有数据。
3 年多之前 回复
dongzhanlian6289
dongzhanlian6289 看到更新的答案。 你可以这样做参考代码。 我在电脑前会更新具体的答案。
3 年多之前 回复
dongmen1925
dongmen1925 我只是想知道在我声明它的值之后我将把postParam放在哪里?
3 年多之前 回复
dongyuntao2000
dongyuntao2000 别客气。 是user_id = \(getUserId.text!)&operation = select是对的兄弟。
3 年多之前 回复
doufei8691
doufei8691 谢谢您的回答。 我只是想知道在我声明它的值之后我将把postParam放在哪里? 另外我有一个user_id的特定值,基于我的Swift代码上的“getUserId.text”,这个代码可以用于postParam中的代码:“user_id =(getUserId.text)&operation = select”
3 年多之前 回复
duanfen1992
duanfen1992 谢谢您的回答。 我只是想知道在我声明它的值之后我将把postParam放在哪里?
3 年多之前 回复
立即提问
相关内容推荐