如何将变量从Swift传递到PHP以用于mySQL查询?

时间:2015-03-26 15:18:08

标签: php ios mysql swift get

我目前正在制作iOS Swift应用程序,该应用程序允许用户创建派对播放列表,并让其他用户加入该派对播放列表以对歌曲进行投票。

我目前很难搞清楚如何将用户输入的文本字段作为变量并将它们传递给PHP脚本,当用户在输入值后点击“CREATE PARTY”按钮时,该脚本会使用这些给定变量执行查询。 / p>

以下是我的ViewController类:

import UIKit
import Foundation

class CreatePartyViewController: UIViewController, NSURLConnectionDelegate, NSURLConnectionDataDelegate {

@IBOutlet weak var partyName: UITextField!
@IBOutlet weak var pinNumber: UITextField!
@IBOutlet weak var host: UITextField!
let genre = "pop"


override func viewDidLoad() {
    super.viewDidLoad()
}



func textFieldShouldReturn(textfield: UITextField!) ->Bool {
    partyName.resignFirstResponder()
    return true
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    self.view.endEditing(true)
}


@IBAction func createParty(sender: AnyObject) {

    // THIS IS WRONG, I AM STUCK FIGURING OUT WHAT TO PUT HERE

    let url = NSString(format:"http://myurl/createParty.php?partyName=\(self.partyName.text)&PIN=\(self.pinNumber.text)&host=\(self.host.text)&genre=\(self.genre)")
    println(url)

    let urlData = NSData(contentsOfURL: url)

    let datastring = NSString(data: urlData!, encoding: UInt())

    println(datastring)


}

这是我的PHP脚本:

$dbhost = "#####";
$dbuser = "#######";
$dbpass = "########";
$db = "########";
$partyTable = "party";
$votesTable = "votes";
$songsTable = "songs";


$conn=mysql_connect($dbhost, $dbuser, $dbpass) or die (mysqli_error());
mysql_select_db($db, $conn) or die(mysql_error());


if (isset ($_GET["partyName"]) && isset ($_GET['PIN']) && isset ($_GET['maxofQ']) && isset ($_GET['genre']))

    {
        $name = $_GET['partyName'];
        $PIN = $_GET['PIN'];
        $host = $_GET['host'];
        $genre = $_GET['genre'];

    }
else
    {
        $name = "nilio";
        $PIN = "000";
        $host = "nilio";
        $genre = "nilio";
    }

$sql1 = "insert into party(artist, PIN, host, genre) values($name, $PIN, $host, $genre)";
$sql2 = "select trackURI, artist, track into votes(trackURI, artist, track) from songs where genre = $genre";
$res1 = mysql_query($sql1, $conn) or die(mysql_error());
$res2 = mysql_query($sql2, $conn) or die(mysql_error());

mysqli_close($conn);    

if ($res) and ($res2)
    {
        echo "success";
    }
else
    {
        echo "failed";
    }

1 个答案:

答案 0 :(得分:0)

我认为您需要使用mysqli而不是mysql

可以使用快速代码,但请检查您的PHP

您也可以查看此主题

POST data to a PHP method from Swift