dragon321723 2016-02-23 06:49
浏览 68
已采纳

如何将数据从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

<?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 enter image description here

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

  • 写回答

3条回答 默认 最新

  • doucan1996 2016-02-25 01:06
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵