从Swift将数据发布到PHP方法 [英] POST data to a PHP method from Swift

查看:194
本文介绍了从Swift将数据发布到PHP方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Swift向我的PHP文件中发布一些信息. 我的php文件已执行,但发布的变量只是不进入php文件.我在做什么错了?

I'm trying to post some info to my PHP file from Swift. My php file is executed, but the posted variables just don't get through to the php file. What am I doing wrong?

快捷代码:

@IBAction func buttonPress(sender: AnyObject) {

        let request = NSMutableURLRequest(URL: NSURL(string: "http://www.domain.com/php_swift_test/insert.php")!)
        request.HTTPMethod = "POST"

        let postString = "a=test&b=bla"
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in

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

            print("response = \(response)")

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()
    }

PHP代码:

<?php
    @session_start();
    @ob_start();

    $host='localhost';
    $user='test';
    $password='Passw0rd99';
    $db_name="mysql_test"; 

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

    $a = $_POST['a'];
    $b = $_POST['b'];

    if(!$connection){
        die('Connection Failed');
    }
    else{
        $dbconnect = @mysql_select_db($db_name, $connection);

        if(!$dbconnect){
            die('Could not connect to Database');
        }
        else{
            $query = "INSERT INTO res_club (FirstName, LastName) VALUES ('$a','$b')";
            mysql_query($query, $connection) or die(mysql_error());

            echo 'Successfully added.';
            echo $query;
            echo $a.$b;
        }
    }
?>

一个空行被添加到数据库中,没有名字和姓氏. PHP文件未获取$_Post['a']和b

An empty row is added to the database, with no first name and last name. The PHP file doesn't get the $_Post['a'] and b

echo语句echo $a.$b也保持空白.没有显示错误.

The echo statement, echo $a.$b stays blank too. No errors are shown.

推荐答案

这对我有用.

视频- https://youtu.be/wYkZ47Rz8iU

快速代码-示例

let request = NSMutableURLRequest(URL: NSURL(string: "http://www.kandidlabs.com/YouTube/SwiftToMySQL/insert.php")!)
        request.HTTPMethod = "POST"
        let postString = "a=\(usernametext.text!)&b=\(password.text!)&c=\(info.text!)&d=\(number.text!)"
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in

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

            print("response = \(response)")

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()

PHP代码-示例

 <?php
    $host='localhost';
    $user='root';
    $password='';

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

    $usernmae = $_POST['a'];
    $pass = $_POST['b'];
    $info = $_POST['c'];
    $num = $_POST['d'];

    if(!$connection)
    {
        die('Connection Failed');
    }
    else
    {
        $dbconnect = @mysql_select_db('YoutubeTutorialDB', $connection);

        if(!$dbconnect)
        {
            die('Could not connect to Database');
        }
        else
        {
            $query = "INSERT INTO `YoutubeTutorialDB`.`Users` (`Username`, `Password`, `Info`, `FavoriteNumber`)
                VALUES ('$username','$pass','$info','$num');";
            mysql_query($query, $connection) or die(mysql_error());

            echo 'Successfully added.';
            echo $query;
        }
    }
?>

这篇关于从Swift将数据发布到PHP方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆