英文:
I/flutter (27225): FormatException: Unexpected character (at character 1) I/flutter (27225): <br /> I/flutter (27225): ^
问题
我想将用户的注册数据插入到我的数据库中。我使用以下代码来执行此操作。但是,我遇到了这个问题,我不理解问题是什么以及解决方案是什么。有人可以帮助我吗?
这是我的注册类中的函数:
TextEditingController email = TextEditingController();
TextEditingController password = TextEditingController();
TextEditingController name = TextEditingController();
TextEditingController username = TextEditingController();
TextEditingController cPassword = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool isRememberMe = false;
validateUserEmail() async {
try {
var res = await http.post(Uri.parse(API.validateEmail), body: {
'e-mail': email.text.trim(),
});
if (res.statusCode == 200) {
//连接成功
var resBody = await jsonDecode(res.body);
if (resBody['emailFound'] == true) {
Fluttertoast.showToast(
msg: "Email已经被使用,请尝试另一个邮箱");
} else {
//注册并保存
registerAndSaveUserrecord();
}
}
} catch (e) {
print(e.toString());
}
}
registerAndSaveUserrecord() async {
try {
var headers = {'Content-Type': 'application/json'};
var url = Uri.parse(API.register);
Map body = {
'username': username.text,
'email': email.text.trim(),
'name': name.text,
'password': password.text,
'cPassword': cPassword.text,
};
http.Response response =
await http.post(url, body: jsonEncode(body), headers: headers);
if (response.statusCode == 200) {
//连接成功
final json = jsonDecode(response.body);
if (json['success'] == true) {
Fluttertoast.showToast(msg: "注册成功!");
} else {
Fluttertoast.showToast(msg: "发生错误,请重试!");
}
}
} catch (e) {
print(e.toString());
}
}
我在用户点击注册按钮时使用这个函数。这是我的注册的PHP文件:
<?php
include '../connection.php';
//POST(将数据发送/保存到mysql)
//GET(从mysql检索/读取)
$username = $_POST['username'];
$email = $_POST['email'];
$name = $_POST['name'];
$password = md5($_POST['password']); //将密码转为二进制以提高安全性
$cPassword = md5($_POST['cPassword']);
$sql = "INSERT * INTO user WHERE email = '$email', name = '$name', username = '$username', password = '$password', cPassword = '$cPassword'";
$result = $connect->query($sql);
if ($result) {
echo json_encode(array("success" => true));
} else {
echo json_encode(array("success" => false));
}
我对Flutter还不太熟悉,所以有点困惑。
当我执行 print(res.body)
时,出现了以下内容。这是什么意思?
英文:
I want to insert registration data from user into my database. I use this code to do so. However, I got this problem, and I don't understand what the problem is as well the solution. Anyone can help me?
This is my function in register class
TextEditingController email = TextEditingController();
TextEditingController password = TextEditingController();
TextEditingController name = TextEditingController();
TextEditingController username = TextEditingController();
TextEditingController cPassword = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool isRememberMe = false;
validateUserEmail() async {
try {
var res = await http.post(Uri.parse(API.validateEmail), body: {
'e-mail': email.text.trim(),
});
if (res.statusCode == 200) {
//connection successful
var resBody = await jsonDecode(res.body);
if (resBody['emailFound'] == true) {
Fluttertoast.showToast(
msg: "Email already been used. Please try another email");
} else {
//register & save
registerAndSaveUserrecord();
}
}
} catch (e) {
print(e.toString());
}
}
registerAndSaveUserrecord() async {
try {
var headers = {'Content-Type': 'application/json'};
var url = Uri.parse(API.register);
Map body = {
'username': username.text,
'email': email.text.trim(),
'name': name.text,
'password': password.text,
'cPassword': cPassword.text,
};
http.Response response =
await http.post(url, body: jsonEncode(body), headers: headers);
if (response.statusCode == 200) {
//connection successful
final json = jsonDecode(response.body);
if (json['success'] == true) {
Fluttertoast.showToast(msg: "Register Successful!");
} else {
Fluttertoast.showToast(msg: "Error Occurred. Try Again!");
}
}
} catch (e) {
print(e.toString());
}
}
I use this function when user click the sign up button.
Here's my php file for register.
<?php
include '../connection.php';
//POST (send/save data to mysql)
//GET (retrieve/read from mysql)
$username = $_POST['username'];
$email = $_POST['email'];
$name = $_POST['name'];
$password = md5($_POST['password']); //pass to binary for secure purpose
$cPassword = md5($_POST['cPassword']);
$sql = "INSERT * INTO user WHERE email = '$email' , name = '$name' , username = '$username' , password = '$password' , cPassword = '$cPassword'";
$result = $connect => query($sql);
if($result){
echo json_encode(array("success" => true ));
}
else {
echo json_encode(array("success" => false ));
}
I'm new with flutter so it's kinda confusing.
This came out when I do print(res.body)
. What does that means?
答案1
得分: 0
同样的问题发生在我身上,我为我的数据库创建了一个新用户,并在PHP文件中用我的新用户名和密码替换了用户名和密码,对我来说一切都正常工作。
英文:
Same problem happened with me, I create new user for my database, and replace user name and password with my newly created user name and password in php file, and it worked fine for me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论