ios 导入mysql数据库,如何将数据从iOS应用程序发布到MySQL数据库?

I saw a similar post to my question but his solution did not work for me for some odd reason and it is making me age faster than Obama.

Basically I want to post data from an iOS app to a MySQL database.

iOS code

NSString *strURL = [NSString stringWithFormat:@"http://www.example.com/phpfile.php?dishname=%@&description=%@",textfieldTwo.text, textfieldTwo.text];

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

NSString *strResults = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@", strResults);

PHP code

$servername="localhost";

$username="admin";

$password="admin";

$dbname="dbname";

$conn=mysqli_connect($servername, $username, $password, $dbname);

if (!$conn)

{

die("Connection failed: " . mysqli_connect_error());

}

$dishname=$_POST['dishname'];

$description=$_POST['description'];

echo "Name : " . $dishname;

//echo "Mail : ". $mail;

$sql="insert into RecipeFeed (DishName, Description) values ('" . $dishname . "', '" . $description . "')";

$result=mysqli_query($conn, $sql);

?>

database image

54b622e7300e64c0ab4f6d0814b56ac6.png

I have no idea whatsoever to why I am having this problem. Any help will be appreciated, thank you!

解决方案

I figured it out and here is the code below.

PHP

// Create connection

$servername = "localhost";

$username = "admin";

$password = "admin";

$dbname = "db";

$con=mysqli_connect("localhost","admin","admin","db");

if (!$con) {

die("Connection failed: " . mysqli_connect_error());

echo "Nothing happened";

}else{

}

if (isset ($_GET["firstname"]))

$firstname = $_GET["firstname"];

else

$firstname = "Null";

if (isset ($_GET["lastname"]))

$lastname = $_GET["lastname"];

else

$lastname = "Null";

if (isset ($_GET["email"]))

$email = $_GET["email"];

else

$email = "Null";

if (isset ($_GET["password"]))

$password = $_GET["password"];

else

$password = "Null";

if (isset ($_GET["timestamp"]))

$timestamp = $_GET["timestamp"];

else

$timestamp = "Null";

$id = "null";

echo "FirstName : ". $firstname;

echo "LastName : ". $lastname;

$sql = "insert into Users (FirstName, ID, LastName, Email, Password, TimeStamp) values ('".$firstname."', '".$id."', '".$lastname."', '".$email."', '".$password."', '".$timestamp."')";

$result = mysqli_query($con, $sql);

?>

iOS

NSString *strURL = [NSString stringWithFormat:@"http://www.example.com/register.php?firstname=%@&lastname=%@&password=%@&email=%@&",firstName.text, lastName.text, password.text, email.text];

NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

NSLog(@"%@", strResult);

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值