The pieces:
The next step is to create a new database, db_example, and a table, example_table.
This table has an id field and five CHAR fields. Other than the id field these could be any SQL table fields.
Create a Main.storyboard as you need to collect the data stored. This is just a demo app so there are just five input, textfields and labels with one button. You may be gathering the data is some other more elaborate method.
Link up the textfields as outlets and the button as an action in ViewController.swift.
import UIKit class ViewController: UIViewController { // The textfield outlets @IBOutlet weak var item1: UITextField! @IBOutlet weak var item2: UITextField! @IBOutlet weak var item3: UITextField! @IBOutlet weak var item4: UITextField! @IBOutlet weak var item5: UITextField! // the button action function @IBAction func uploadData(_ sender: Any) { let url = NSURL(string: "http://localhost:8888/sdb/receive.php") // locahost MAMP - change to point to your database server var request = URLRequest(url: url!) request.httpMethod = "POST" var dataString = "secretWord=44fdcv8jf3" // starting POST string with a secretWord // the POST string has entries separated by & dataString = dataString + "&item1=\(item1.text!)" // add items as name and value dataString = dataString + "&item2=\(item2.text!)" dataString = dataString + "&item3=\(item3.text!)" dataString = dataString + "&item4=\(item4.text!)" dataString = dataString + "&item5=\(item5.text!)" // convert the post string to utf8 format let dataD = dataString.data(using: .utf8) // convert to utf8 string do { // the upload task, uploadJob, is defined here let uploadJob = URLSession.shared.uploadTask(with: request, from: dataD) { data, response, error in if error != nil { // display an alert if there is an error inside the DispatchQueue.main.async DispatchQueue.main.async { let alert = UIAlertController(title: "Upload Didn't Work?", message: "Looks like the connection to the server didn't work. Do you have Internet access?", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil)) self.present(alert, animated: true, completion: nil) } } else { if let unwrappedData = data { let returnedData = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue) // Response from web server hosting the database if returnedData == "1" // insert into database worked { // display an alert if no error and database insert worked (return = 1) inside the DispatchQueue.main.async DispatchQueue.main.async { let alert = UIAlertController(title: "Upload OK?", message: "Looks like the upload and insert into the database worked.", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil)) self.present(alert, animated: true, completion: nil) } } else { // display an alert if an error and database insert didn't worked (return != 1) inside the DispatchQueue.main.async DispatchQueue.main.async { let alert = UIAlertController(title: "Upload Didn't Work", message: "Looks like the insert into the database did not worked.", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil)) self.present(alert, animated: true, completion: nil) } } } } } uploadJob.resume() } } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
<?php $secret = $_POST["secretWord"]; if ("44fdcv8jf3" != $secret) exit; // note the same secret as the app - could be let out if this check is not required. secretWord is not entered by the user and is used to prevent unauthorized access to the database $item1 = $_POST['item1']; $item2 = $_POST['item2']; $item3 = $_POST['item3']; $item4 = $_POST['item4']; $item5 = $_POST['item5']; // POST items should be checked for bad information before being added to the database. // Create connection $mysqli=mysqli_connect("127.0.0.1","db_user","password","db_example"); // localhost, user name, user password, database name // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $query = "insert into `example_table` (item1, item2, item3, item4, item5) value ('".$item1."','".$item2."','".$item3."','".$item4."','".$item5."')"; $result = mysqli_query($mysqli,$query); echo $result; // sends 1 if insert worked ?>