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(),
);
}
and 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";
}
?>
and error:
[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...
json.decode(response.body);when your PHP just ouputs strings, not json. Either remove thejson.decode()or output the result as valid json.json.decode()but still it doesn't post anything to database...<?since they have been disabled as default since a bunch of versions back. Use<?php. What does the$funcvariable contain? PDO? MySQLi? A custom DB class? Do you have any error logging for failed queries? Also, since you're not escaping the post data at all before injecting it into the query, a single'would break your query. Please share all relevant PHP code. If you post to your PHP endpoint directly from Postman or similar, what do you get? Have you checked the web servers error log? Please share all relevant code and debugging info.