0

I'm new to the ios programming and i'm trying to connect the ios app to mysql server and i'm getting the error as below i have posted please help me regarding this. below is my php script

<?php
            $host = ’localhost’;
            $user = ‘’;
            $password = ‘’;


            $connection = mysqli_connect($host,$user,$password);


            $fname = $_POST[‘a’];
            $lname = $_POST[‘b’];
            $school = $_POST[‘c’];

            if(!$connection){
                            die(‘connection failed’);
            }
            else{
                    $dbconnect = @mysqli_select_db(‘pnallama’,$connection);

                    if(!$dbconnect){
                                    die(‘connection to DB failed’);
                    }
                    else{
                            $query = “INSERT INTO ‘pnallama’.’DataCollector’ (‘firstname’, ‘lastname’, ‘school’) VALUES (‘$fname’, ‘$lname’, ‘$school’);”;
                            mysqli_query($query, $connection) or die(mysqli_error());

                            echo ‘successfully added’;
                            echo $query;
                    }
            }

?>

and my view controller is

import UIKit

class CollectorDetailView: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textField1: UITextField! = nil
@IBOutlet weak var textField2: UITextField! = nil
@IBOutlet weak var textField3: UITextField! = nil

@IBAction func submitBtnPressed(_ sender: UIButton) {
    let request = NSMutableURLRequest(url: NSURL(string: "http://kindlewell.com/~pnallama/insert.php")! as URL)
    request.httpMethod = "POST"
    let postString = "a=\(textField1.text!)&b=\(textField2.text!)&c=\(textField3.text!)"
    request.httpBody = postString.data(using: String.Encoding.utf8)

    let task = URLSession.shared.dataTask(with: request as URLRequest) {data, response, error in

        if error != nil {
            print("error=\(error)")
            return
        }

        print("response=\(response)")

        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print("responseString =\(responseString)")
        }
    task.resume()

    }

override func viewDidLoad() {
    super.viewDidLoad()
    textField1.delegate = self
    textField2.delegate = self
    textField3.delegate = self

}

func textFieldShouldReturn(_ textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
{
    textField.resignFirstResponder()
    return true;
}

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

}

and i'm getting an error like this

response=Optional(<NSHTTPURLResponse: 0x60800022ad60> { URL: http://kindlewell.com/~pnallama/insert.php } { status code: 500, headers {
Connection = close;
"Content-Length" = 0;
"Content-Type" = "text/html";
Date = "Wed, 12 Oct 2016 06:31:26 GMT";
Server = "Apache/2.4.6 (Linux/SUSE)";
"X-Powered-By" = "PHP/5.4.20, Mono";
} })
(lldb) 
Kilian Stinson
  • 2,376
  • 28
  • 33

1 Answers1

0

try replacing double quotes with single quote and try naming the table in query without quote I had the same problem once and then found out it was comming because I put table name inside quotes try doing it and let me know if it helps

ajay mishra
  • 80
  • 10
  • This answer is erroneous. What happens if you swap double quotes for single quotes in the following: `"INSERT INTO foo (bar) VALUES ('baz')";` ? If you look at the OPs quotes they are not standard double quotes. It's as if they have composed their code in something like MS Word. – Progrock Oct 12 '16 at 07:38
  • Progrock i have changed all the single quotes. – Prudhvi Nallamanikaluva Oct 12 '16 at 07:48
  • well I was just giving suggestion, sorry if it didn't work . mine was working after I removed the name from 'db'.'tablename' to db.tablename – ajay mishra Oct 12 '16 at 07:50