从Swift将数据发布到PHP方法

编程入门 行业动态 更新时间:2024-10-24 15:16:50
本文介绍了从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: "www.domain/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.

推荐答案

这对我有用.

视频- youtu.be/wYkZ47Rz8iU

快速代码-示例

let request = NSMutableURLRequest(URL: NSURL(string: "www.kandidlabs/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方法

本文发布于:2023-11-26 18:32:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634704.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   数据   Swift   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!