FormatException在从Flutter应用程序发送数据到PHP后端时发生。

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

FormatException when posting data from Flutter app to PHP back-end

问题

我正在开发一个Flutter应用程序,该应用程序从控制器获取文本。在提交后,它执行一个将数据发送到PHP后端的函数,然后将数据插入到MySQL数据库中。

我遇到的问题是一个未处理的异常:“FormatException:意外字符(在第1个字符处)”。

我目前只是尝试更新表中的名字列,ID设置为自动增加。我不确定是什么原因导致了这个错误消息。

这是我的Dart函数,用于发送数据:

postData() async {
    name = nameField.text;
    lastName = lastField.text;

    var urlWeb = Uri.parse('https://sqlfinalproject.000webhostapp.com/insert.php');

    var res = await http.post(urlWeb, headers: {"Accept": "application/json"},
        body: {
          "name": name,
          "last": lastName,
        }
    );

这是插入数据的PHP代码:

<?php
require("db.php");

$firstName = $_POST["name"];
$lastName = $_POST["last"];

$querySt = "INSERT INTO project1 (id,firstName,lastName) VALUES(NULL,'".$firstName."','".$lastName."')";

$statement = $connection->prepare($querySt);

$statement->execute();

echo json_encode("inserted data");

?>

如果有关于可能导致此错误的见解,将不胜感激。

英文:

I am developing a Flutter application that takes text from a controller. Upon submitting, it executes a function that posts the data to a PHP back-end, which then inserts the data into a MySQL database.

The problem I'm encountering is an unhandled exception: "FormatException: Unexpected character (at character 1)".

I am currently just trying to update the first name column in the table, with the ID set to auto increment. I am unsure what is causing this error message.

Here's my Dart function that posts the data:

postData () async {
    name = nameField.text;
    lastName = lastField.text;

    var urlWeb = Uri.parse(&#39;https://sqlfinalproject.000webhostapp.com/insert.php&#39;);

    var res = await http.post(urlWeb ,headers: {&quot;Accept&quot;:&quot;application/json&quot;},
        body: {
          &quot;name&quot;:name,
          &quot;last&quot;:lastName,
        }
    );

And here's the PHP code that inserts the data:

&lt;?php
require(&quot;db.php&quot;);

$firstName = $_POST[&quot;name&quot;];
$lastName = $_POST[&quot;last&quot;];

$querySt = &quot;INSERT INTO project1 (id,firstName,lastName) VALUES(&#39;NULL,&quot;.$firstName.&quot;&#39;,&#39;&quot;.$lastName.&quot;&#39;)&quot;;

$statement = $connection-&gt;prepare($querySt);

$statement-&gt;execute();

echo json_encode(&quot;inserted data&quot;);

?&gt;

Any insights as to what could be causing this error would be much appreciated.

答案1

得分: 1

你的错误来自于试图将一个非 JSON / 数组 / 对象(即字符串)转换为 JSON 并输出它。因此错误出在这里。

echo json_encode("插入的数据");

在这一点上,最好传递一个关联数组。

echo json_encode(array("message" => "插入的数据"));

除此之外,你的 SQL 查询也有问题。在 SQL 查询的 VALUES 部分,你写了 'NULL,".$firstName.",".$lastName.'。在 NULL 前面不应该有单引号,而且在 $firstName 前面缺少一个单引号。

$querySt = "INSERT INTO project1 (id,firstName,lastName) 
VALUES(NULL,'".$firstName."','".$lastName."')";

你不应该直接将来自用户的值直接写入数据库,至少要进行验证。查看 PDO 准备绑定语句

英文:

Your error comes from trying to convert a non JSON / array / object, respectively a string, to a json and output it. So the error is here.

echo json_encode(&quot;inserted data&quot;);

At this point it would probably be better to pass an assiocative array

echo json_encode(array(&quot;message&quot; =&gt; &quot;inserted data&quot;));

Besides this, there is a problem with your SQL query as well. In the VALUES part of the SQL query, you have written 'NULL,".$firstName."','".$lastName."'. There shouldn't be single quotes around NULL, and you're missing a single quote before $firstName.

$querySt = &quot;INSERT INTO project1 (id,firstName,lastName) 
VALUES(NULL,&#39;&quot;.$firstName.&quot;&#39;,&#39;&quot;.$lastName.&quot;&#39;)&quot;;

You should never write values that come from the user directly into the database without at least validating them.
Have a look at the PDO perpare bind statements.

huangapple
  • 本文由 发表于 2023年7月24日 00:16:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749228.html
匿名

发表评论

匿名网友

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

确定