使用 iOS 前端插入 MYSQL 数据库的空白数据 [英] Blank data inserted into MYSQL database with iOS frontend

查看:58
本文介绍了使用 iOS 前端插入 MYSQL 数据库的空白数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating an app which requires to insert a row into a mysql database (hosted online) however whenever i try to insert the data, the data is blank. there is nothing wrong with the php as my web front end works perfectly with the php script.

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

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有关?

also, responseString usually shows up with some weird characters after it therefore affect the responseString.length value. Is there something wrong with my code or is it to do with my php?

推荐答案

也许 PHP 没有解析请求正文,因为您尚未设置 Content-Type 请求标头:

Maybe PHP is not parsing the request body because you haven't set the Content-Type request header:

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

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

On the other hand I recommend you to use an HTTP library of iOS such as AFNetworking so you don't have to worry about generating the request body by hand. Check out its documentation.

这篇关于使用 iOS 前端插入 MYSQL 数据库的空白数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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