Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我希望在单击按钮时将我的UISlider值发送到mysql数据库_Php_Mysql_Swift_Xcode_Uislider - Fatal编程技术网

Php 我希望在单击按钮时将我的UISlider值发送到mysql数据库

Php 我希望在单击按钮时将我的UISlider值发送到mysql数据库,php,mysql,swift,xcode,uislider,Php,Mysql,Swift,Xcode,Uislider,我希望能够将我的swift项目连接到我的数据库(使用phpmyadmin托管在blue host上),当单击按钮时,我希望滑块或标签上的值(因为它们已连接)输入到我数据库中的表中。我知道这是一个由两部分组成的问题,但我很难找到有用的资源: 使用php和phpmyadmin将swift 3连接到我的数据库(我找不到一个完整的教程来解释将文件放在何处以及如何访问它们) 及 如何将该值发送到数据库 我将增加更多,但要在我的项目上取得一些突破,我真的需要一个坚实的连接开始,并找出如何发送数据和检索它与s

我希望能够将我的swift项目连接到我的数据库(使用phpmyadmin托管在blue host上),当单击按钮时,我希望滑块或标签上的值(因为它们已连接)输入到我数据库中的表中。我知道这是一个由两部分组成的问题,但我很难找到有用的资源:

  • 使用php和phpmyadmin将swift 3连接到我的数据库(我找不到一个完整的教程来解释将文件放在何处以及如何访问它们)
  • 如何将该值发送到数据库 我将增加更多,但要在我的项目上取得一些突破,我真的需要一个坚实的连接开始,并找出如何发送数据和检索它与swift和数据库

    我真的很感谢在这个问题上的任何帮助,甚至是任何可能帮助我的资源的链接。我是编程新手,所以这一切对我来说都是陌生的

    // So far this is my code in [view controller.swift]
    
    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var lbl: UILabel!
    
        @IBAction func slider(_ sender: UISlider)
        {
            lbl.text = String(Int(sender.value))
        }
    
        func postToServerFunction() {
            print(lbl.text!)
        }
    
        @IBAction func postToServerButton(_ sender: UIButton) {
            postToServerFunction()
        }
    
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
        }
    
        override func didReceiveMemoryWarning()
        {
            super.didReceiveMemoryWarning()
        }
    }
    

    这是一个很大的问题,所以很难一下子回答,但鉴于没有其他人尝试过,让我试着帮你一点忙

    我看到您已经有了一个函数“postToServerButton”,我假设它连接到故事板中的“Button”按钮。这是在调用postToServerFunction(),我看不出它是书面的。我先写下来。您还需要将滑块作为IBOutlet连接到代码以获取其值。我是从内存中写这段代码的,所以其中一些可能是错误的,但希望你能理解

    @IBOutlet weak var slider: UISlider! // Connect your slider to this
    let constantValue = 8 // Don't change this
    
    func postToServerFunction() {
        var sliderValue = Int(self.slider.value) // The slider value as an Int
        var sliderString = String(sliderValue) // The slider value as a String
        // The line below is to help you check that you're getting the right values from the slider
        print("\(self.constantValue)==D, Slider value: \(sliderValue), Slider String: \(sliderString)")
    
        postValueToServer(sliderValue) // We'll write this below, this is where the post to server part is happening
    }
    
    所以下一个函数是事情变得复杂的地方。这是将代码连接到数据库。我在下面写了一些基本代码,但这是一个复杂的主题,取决于各种变量

    func postValueToServer(sliderValue : Int) {
        print("\(self.constantValue)==D, Posting slider value \(sliderValue) to database") // Again just another check to make sure we're posting the right value
        var url: NSURL = NSURL(string: "http://INSERTYOURPHPURL.php")!
        var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
        request.HTTPMethod = "POST"
        request.HTTPBody = sliderValue.dataUsingEncoding(NSUTF8StringEncoding);
        NSURLConnection.sendAsynchronousRequest(request, queue:       NSOperationQueue.mainQueue())
            {
                (response, data, error) in
                print(response)
    
            }   
    }
    
    请访问此网站,以获取一个好的指南: