英文:
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('https://sqlfinalproject.000webhostapp.com/insert.php');
var res = await http.post(urlWeb ,headers: {"Accept":"application/json"},
body: {
"name":name,
"last":lastName,
}
);
And here's the PHP code that inserts the data:
<?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");
?>
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("inserted data");
At this point it would probably be better to pass an assiocative array
echo json_encode(array("message" => "inserted data"));
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 = "INSERT INTO project1 (id,firstName,lastName)
VALUES(NULL,'".$firstName."','".$lastName."')";
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论