从Swift向PHP SQL脚本传递变量时出现问题

从Swift向PHP SQL脚本传递变量时出现问题,php,ios,mysql,swift,Php,Ios,Mysql,Swift,我应该在Swift代码中添加什么来将变量传递到下面的PHP脚本中? 我的PHP脚本正在等待$ID=$\u POST['customerName'中的值customerName 如何在Swift代码中硬编码值以发送值 Swift代码: import Foundation protocol FeedDetailProtocol: class { func itemsDownloaded(items: NSArray) } class FeedDetail: NSObject, URLS

我应该在Swift代码中添加什么来将变量传递到下面的PHP脚本中? 我的PHP脚本正在等待
$ID=$\u POST['customerName'中的值
customerName

如何在Swift代码中硬编码值以发送值

Swift代码:

import Foundation

protocol FeedDetailProtocol: class {
    func itemsDownloaded(items: NSArray)
}


class FeedDetail: NSObject, URLSessionDataDelegate {



    weak var delegate: FeedDetailProtocol!



func downloadItems() {


    let url = URL(string: "https://www.example.com/test/test1.php")!
    let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
    var request = URLRequest(url: url)
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "POST"**strong text**
    let parameters: [String: Any] = ["customerName": "John"]

    request.httpBody = parameters.percentEscaped().data(using: .utf8)

    let task = defaultSession.dataTask(with: url) { (data, response, error) in

        if error != nil {
            print("Error")
        }else {
            print("details downloaded")
            self.parseJSON(data!)
        }

    }

    task.resume()
}

    func parseJSON(_ data:Data) {

        var jsonResult = NSArray()

        do{
            jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray

        } catch let error as NSError {
            print(error)

        }

        var jsonElement = NSDictionary()
        let stocks = NSMutableArray()

        for i in 0 ..< jsonResult.count
        {

            jsonElement = jsonResult[i] as! NSDictionary

            let stock = DetailModel()

            //the following insures none of the JsonElement values are nil through optional binding
            if let rma = jsonElement["rma"] as? String,
                let customer = jsonElement["customer"] as? String,
                let manufacturer = jsonElement["manufacturer"] as? String,
                let model = jsonElement["model"] as? String

            {

                stock.rma = rma
                stock.manufacturer = manufacturer
                stock.model = model
                stock.customer = customer

            }

            stocks.add(stock)

        }

        DispatchQueue.main.async(execute: { () -> Void in

            self.delegate.itemsDownloaded(items: stocks)

        })
    }
}
$con=mysqli_connect("localhost","username","password","dbName");

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This SQL statement selects ALL from the table 'Equipment'

$ID = $_POST['customerName'];

$sql = "SELECT customer, rma, manufacturer, model, status FROM Equipment 
        WHERE customer = '$ID' ";

// Check if there are results
if ($result = mysqli_query($con, $sql))
{
    // Create temporary connection
    $resultArray = array();
    $tempArray = array();

    // Look through each row
    while($row = $result->fetch_object())
    {
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}

mysqli_close($con);

PHP代码采用POST值($ID=$\u POST['customerName']),因此需要创建POST请求

大概是这样的:

let url = URL(string: "https://www.example.com/test1/test1.php")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let parameters: [String: Any] = ["customerName": "John"]`

您是收到错误,还是没有得到值?我这样说是因为您发布的代码有一个语法错误,这将导致PHP中出现500个错误(
$sql=“从设备中选择客户、rma、制造商、型号,其中客户='$ID';
应该是
$sql=“从设备中选择客户、rma、制造商、型号,其中客户='$ID'“;
-字符串末尾缺少尾随双引号)。@ChrisWhite该值未从Swift应用程序传递到phpscript@ChrisWhite缺少尾随双引号是我的错,复制时我忘了将其添加到我的帖子中。但是这个问题还没有解决,你能看一下吗,这个问题肯定是Swift部分对不起,我一点也不知道Swift。我更新了我的帖子,你能看一下吗?我真的很感激func“itemsDownloaded”在哪里实现