gpt4 book ai didi

php - FLUTTER&PHP-我无法从Flutter应用发布到mysql数据库

转载 作者:行者123 更新时间:2023-12-03 03:33:06 25 4
gpt4 key购买 nike

我正在尝试从我的flutter应用程序连接数据库,我想将在文本字段中编写的值发布到数据库中。我写了一些代码,但无法发布到数据库,这给了我错误。我想我必须编辑我的PHP代码,但我不知道该如何编辑,请帮助我...下面的代码和错误

     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<…>
我做错了,但我不知道...

最佳答案

看一下您的API,我认为您正在尝试发布数据。同时,我可以看到您在flutter应用程序中使用的是get请求。
如果您尝试发布数据,请从flutter应用程序发出POST请求,而不是GET请求。
这是一个使用HTTP包的POST请求示例。
样本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}");
}

关于php - FLUTTER&PHP-我无法从Flutter应用发布到mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62656875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com