FLUTTER& PHP-我无法从我的Flutter应用发布到mysql数据库 [英] FLUTTER&PHP - I cannot post to mysql database from my flutter app
问题描述
我正在尝试从flutter应用程序连接数据库,我想将在文本字段中编写的值发布到数据库中.我写了一些代码,但无法发布到数据库,这给了我错误.我想我必须编辑我的php代码,但是我不知道如何编辑,请帮助我...下面的代码和错误
I am trying to connect database from my flutter app and I want to post value that I wrote in textfield to database. I wrote some code but I cannot post to database, it is giving me error. I guess I have to edit my php code but I don't know how I can edit, please help me... the codes and errors below here
Future<List> sendData() async {
await http.post(
"https://www.ekspar.com/trying/go.php",
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: {
"adi": nameController.text,
"soyadi": surnameController.text,
},
);
json.decode(response.body);
}
@override
void initState() {
sendData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: Scaffold(
appBar: AppBar(
title: Text("Register"),
),
body: Container(
child: Center(
child: Column(
children: <Widget>[
Text(
"ad",
style: TextStyle(fontSize: 18.0),
),
TextField(
controller: nameController,
decoration: InputDecoration(hintText: 'ad'),
),
Text(
"soyad",
style: TextStyle(fontSize: 18.0),
),
TextField(
controller: surnameController,
decoration: InputDecoration(hintText: 'soyad'),
),
RaisedButton(
child: Text("Register"),
onPressed: () {
setState(() {
_build();
});
sendData();
},
),
_build()
],
),
),
),
)
//(_buildBody(),
);
}
和PHP:
<?
include("begin.php");
include("functions-develop.php");
$adi = $_POST['adi'];
$soyadi =$_POST['soyadi'];
if ($adi and $soyadi) {
$query = $func->query("insert into `dart` (`adi`, `soyadi`) VALUES ('$adi','$soyadi')");
echo "Kayit Eklenmiştir";
}
else
{
echo "Bos veri";
}
?>
和错误:
[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: FormatException: Unexpected character (at character 1)
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
^
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
#1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1271:9)
#2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:936:22)
#3 _parseJson (dart:convert-patch/convert_patch.dart:40:10)
#4 JsonDecoder.convert (dart:convert/json.dart:505:36)
#5 JsonCodec.decode (dart:convert/json.dart:156:41)
#6 jsonDecode (dart:convert/json.dart:96:10)
#7 _LoginScreenState.sendData
package:ekspar/screens/login.dart:357
<asynchronous suspension>
#8 _LoginScreenState.initState
package:ekspar/screens/login.dart:373
#9 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4684
#10 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4520
#11 Element.infl<…>
我做错了,但我不知道...
i am doing wrong but where I don't know...
推荐答案
看看您的API,让我认为您正在尝试发布数据.同时,我可以看到您在flutter应用程序中使用的是get请求.
Taking a look at your API makes me think that you are trying to post data. At the same time, I can see that you are using a get request in your flutter app.
如果您要发布数据,请从flutter应用发出POST请求,而不是GET请求.
If you are trying to post data then make a POST request from your flutter app rather then a GET request.
以下是使用 HTTP 包的扑朔迷离的POST请求示例.
Here's an example of POST request in flutter using the HTTP package.
示例POST请求:
String url = "https://www.ekspar.com.tr/onarim/post.php";
var response = await http.post(url, body: {
"adi":"YOUR_DATA",
"soyadi":"YOUR_DATA"
});
var body = jsonDecode(response.body);
if(response.statusCode == 200){
debugPrint("Data posted successfully");
}else{
debugPrint("Something went wrong! Status Code is: ${response.statusCode}");
}
这篇关于FLUTTER& PHP-我无法从我的Flutter应用发布到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!