如何将数据从iOS应用程序发布到MySQL数据库? [英] How to post data from iOS app to MySQL database?

查看:99
本文介绍了如何将数据从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.

基本上我想要将数据从iOS应用程序发布到MySQL数据库。

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

iOS代码

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代码

<?php

$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

我不知道为什么我遇到这个问题。任何帮助将不胜感激,谢谢!

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

<?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);

这篇关于如何将数据从iOS应用程序发布到MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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