I/flutter (27225): FormatException: Unexpected character (at character 1) I/flutter (27225): <br /> I/flutter (27225): ^

huangapple go评论72阅读模式
英文:

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/flutter (27225): FormatException: Unexpected character (at character 1) I/flutter (27225): <br /> I/flutter (27225): ^

英文:

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&lt;FormState&gt;();
  bool isRememberMe = false;

  validateUserEmail() async {
    try {
      var res = await http.post(Uri.parse(API.validateEmail), body: {
        &#39;e-mail&#39;: email.text.trim(),
      });
      if (res.statusCode == 200) {
        //connection successful
        var resBody = await jsonDecode(res.body);

        if (resBody[&#39;emailFound&#39;] == true) {
          Fluttertoast.showToast(
              msg: &quot;Email already been used. Please try another email&quot;);
        } else {
          //register &amp; save
          registerAndSaveUserrecord();
        }
      }
    } catch (e) {
      print(e.toString());
    }
  }

  registerAndSaveUserrecord() async {
   
    try {
     
      var headers = {&#39;Content-Type&#39;: &#39;application/json&#39;};
      var url = Uri.parse(API.register);
      Map body = {
        &#39;username&#39;: username.text,
        &#39;email&#39;: email.text.trim(),
        &#39;name&#39;: name.text,
        &#39;password&#39;: password.text,
        &#39;cPassword&#39;: 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[&#39;success&#39;] == true) {
          Fluttertoast.showToast(msg: &quot;Register Successful!&quot;);
        } else {
          Fluttertoast.showToast(msg: &quot;Error Occurred. Try Again!&quot;);
        }
      }
    } catch (e) {
      print(e.toString());
    }
  }

I use this function when user click the sign up button.
Here's my php file for register.

&lt;?php

 include &#39;../connection.php&#39;;

 //POST (send/save data to mysql)
 //GET (retrieve/read from mysql)

 $username = $_POST[&#39;username&#39;];
 $email = $_POST[&#39;email&#39;];
 $name = $_POST[&#39;name&#39;];
 $password = md5($_POST[&#39;password&#39;]); //pass to binary for secure purpose
 $cPassword = md5($_POST[&#39;cPassword&#39;]);

 $sql = &quot;INSERT * INTO user WHERE email = &#39;$email&#39; , name = &#39;$name&#39; , username = &#39;$username&#39; ,  password = &#39;$password&#39; , cPassword = &#39;$cPassword&#39;&quot;;

 $result = $connect =&gt; query($sql);

 if($result){

    echo json_encode(array(&quot;success&quot; =&gt; true ));
 }

 else {
    echo json_encode(array(&quot;success&quot; =&gt; false ));
 }

I'm new with flutter so it's kinda confusing.

This came out when I do print(res.body) . What does that means?

I/flutter (27225): FormatException: Unexpected character (at character 1) I/flutter (27225): <br /> I/flutter (27225): ^

答案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.

huangapple
  • 本文由 发表于 2023年1月6日 10:43:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定