2

I am currently making an iOS Swift application that allows a user to create a party playlist and have other users join that party playlist to vote on the songs.

I am currently stuck figuring out how to take the user-input text fields as variables and pass them into a PHP script that executes queries with those given variables when the user taps the button "CREATE PARTY" after entering in values.

Below is my ViewController class:

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)


}

And here is my PHP script:

$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";
    }
Machavity
  • 30,841
  • 27
  • 92
  • 100
Sam Carter
  • 71
  • 3

1 Answers1

0

I think you need to use mysqli not mysql

Swift code is ok but check your php

You can also look this topic

POST data to a PHP method from Swift

Tugrul
  • 31
  • 6