【问题标题】:Blank data inserted into MYSQL database with iOS frontend使用 iOS 前端将空白数据插入 MYSQL 数据库
【发布时间】:2013-09-19 08:56:00
【问题描述】:

我正在创建一个应用程序,它需要在 mysql 数据库(在线托管)中插入一行,但是每当我尝试插入数据时,数据都是空白的。 php 没有任何问题,因为我的 Web 前端与 php 脚本完美配合。

NSString *user = self.registerUsername.text;
NSString *password = self.registerPassword.text;
NSString *email = self.registerEmail.text;
NSString *model = [NSString stringWithFormat:@"%@", [[UIDevice currentDevice] model]];
NSString *sysVer = [NSString stringWithFormat:@"%@", [[UIDevice currentDevice] systemVersion]];

NSString *connectionURL = @"mydomain.com/Register.php";
NSLog(@"%@", connectionURL);
//this is logged correctly

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:connectionURL]];
[request setHTTPMethod:@"POST"];

NSString *insert = [[NSString alloc] initWithFormat:@"username=%@&password=%@&email=%@&model=%@&sysver=%@", user, password, email, model, sysVer];

NSLog(@"%@", insert);
[request setHTTPBody:[insert dataUsingEncoding:NSASCIIStringEncoding]];
//This is also logged correctly, giving me the right format i would expect

NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request                             returningResponse:&response error:&error];

NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(@"%@", responseString);
//The response string is also correct, but no data is inserted into the database

NSString *success = @"success";
[success dataUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%lu", (unsigned long)responseString.length);
NSLog(@"%lu", (unsigned long)success.length);

mRegister.php

<?php

header('Content-type: text/plain; charset=utf-8');

include("config/dp.php"); //<- db connection

$con = mysql_connect("127.0.0.1","user","pass");
if(! $con)
{
die('Connection Failed'.mysql_error());
}

mysql_select_db("hk", $con);

$message = "";

$username = ($_POST['username']); 
$password = ($_POST['password']);
$email = ($_POST['email']);
$model = ($_POST['model']);
$sysver = ($_POST['sysver']);

$submit = $_POST["registerform"];

$sql = "INSERT INTO members (`username`, `password`, `email`, `model`, `sysver`) VALUES ('$username', '$password', '$email', '$model', '$sysver')";
$result = mysql_query($sql);

$row = mysql_fetch_array($result);

if ($result) { $message = "success"; }
else { $message = "failed"; }

echo utf8_encode($message);

?> 

另外,responseString 通常会出现一些奇怪的字符,因此会影响responseString.length 的值。我的代码有问题还是我的 php 有问题?

【问题讨论】:

  • 你说在线插入,不应该连接到服务器而不是本地主机吗?
  • 那只是因为我不想这样做,但服务器的 ip up - 它不同并且有效(我可以验证)
  • 编辑您的帖子以包含缺少的列,这样其他人就不会说同样的话。
  • @Mihai 更新以删除拼写错误等

标签: php mysql ios


【解决方案1】:

也许 PHP 没有解析请求体,因为你没有设置 Content-Type 请求头:

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

另一方面,我建议您使用 iOS 的 HTTP 库,例如 AFNetworking,这样您就不必担心手动生成请求正文。查看its documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多