【问题标题】:Inserting data into online DB将数据插入在线数据库
【发布时间】:2013-02-05 08:11:19
【问题描述】:

我正在尝试向 MySQL DB 中插入一些值,通过 iPad 应用程序在线访问它。

发送数据:

if(txtInput != nil){
        NSString *postVars = [NSString stringWithFormat:@"id=%d&input=%@", index, txtInput.text];
        NSLog(@"%d",index);
        index++;
        NSData *data = [postVars dataUsingEncoding:NSUTF8StringEncoding];

        NSString *strURL = @"http://localhost:8888/Insert.php";
        NSURL *url = [NSURL URLWithString:strURL];
        NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:url];
        [req setHTTPBody:data];
        [req setHTTPMethod:@"POST"];
        NSURLConnection *con = [NSURLConnection connectionWithRequest:req delegate:self];
    }

Web 服务脚本:

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

$con = mysql_connect($host, $username, $password);
if(!$con)
{
echo "Failed connection";
echo "<br/>";
die('Connection failed');
}

mysql_select_db("unbounded", $con);

$ids = $_GET['id'];
$str = $_GET['input'];

$in= mysql_query("INSERT INTO `unbounded`.`stuff` (`id`, `string`) VALUES ('$ids','$str');");
echo $in;
echo "<br/>";
mysql_close($con);
?> 

在 PHPAdmin 页面运行我的应用程序后,我可以看到每次执行应用程序时 id 值都设置为 0,而字符串值为空。

如果我通过浏览器运行脚本来插入数据就好了。顺便说一句,一旦我执行脚本 vi 浏览器,我发现与 DB 的连接已经建立良好。 . 可能是什么问题? 最好的毕业生

【问题讨论】:

    标签: php mysql objective-c sql ipad


    【解决方案1】:

    在您的 iOS 应用中,您使用 POST 方法发送数据

    [req setHTTPMethod:@"POST"];
    

    但是在 PHP 中你是从 GET 读取的

    $ids = $_GET['id'];
    

    如果两者都使用相同的 HTTP 方法,它应该可以工作

    与您的问题无关,但您的 PHP 代码容易受到 SQL 注入 攻击。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-29
      • 2020-07-05
      • 2023-04-10
      • 2021-10-21
      • 2014-08-08
      • 2014-05-04
      相关资源
      最近更新 更多