'Flutter: insert textfield data into the MYSQL database using POST method in php
I try to insert my text field data into the MYSQL database using PHP post method. Here This is My code for SAVE data.
POST method Showing this Undefined index: visitor_name in
database connected successfully GET data works Properly getting data in List View there is no problem in getdata function But save data Funtion Not Working
class Homepage extends StatefulWidget {
const Homepage({Key? key}) : super(key: key);
@override
State<Homepage> createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
@override
Widget build(BuildContext context) {
TextEditingController txtVname = TextEditingController();
TextEditingController txtVnumber = TextEditingController();
TextEditingController txtvemail = TextEditingController();
TextEditingController txtvReason = TextEditingController();
TextEditingController txtename = TextEditingController();
TextEditingController txtRole = TextEditingController();
TextEditingController txtemail = TextEditingController();
bool _valvname = false;
bool _valvnumber = false;
bool _valvemail = false;
bool _valvReason = false;
bool _valename = false;
bool _valRole = false;
bool _valemail = false;
bool visible = false;
Future insertData() async {
setState(() {
visible = true;
});
String visitor_name = txtVname.text;
String visitor_num = txtVnumber.text;
String visitor_email = txtemail.text;
String visitor_Reason = txtvReason.text;
String emp_name = txtename.text;
String emp_Role = txtRole.text;
String emp_email = txtemail.text;
var Data = {
'visitor_name': visitor_name,
'visitor_num': visitor_num,
'visitor_email': visitor_email,
'visitor_Reason': visitor_Reason,
'emp_name': emp_name,
'emp_Role': emp_Role,
'emp_email': emp_email,
};
var url = "https://astatic-scab.000webhostapp.com/vsave.php";
var res = await http.post(Uri.parse(url), body: jsonEncode(Data));
var message = jsonDecode(res.body);
if (message == "true") {
print("Successful " + message);
} else {
print("Error: " + message);
}
}
AND MY PHP CODE
<?php
include "config.php";
$visitor_name=$_POST['visitor_name'];
$visitor_num=$_POST['visitor_num'];
$visitor_email=$_POST['visitor_email'];
$visitor_Reason=$_POST['visitor_Reason'];
$emp_name=$_POST['emp_name'];
$emp_Role=$_POST['emp_Role'];
$emp_email=$_POST['emp_email'];
if($visitor_name!="" && $visitor_num!="" && $visitor_email!="" && $visitor_Reason!="" && $emp_name!="" && $emp_Role!="" && $emp_email!="" ){
$sql="insert into Enquiry (visitor_name,visitor_num,visitor_email,visitor_Reason,emp_name,emp_Role,emp_email) values('$visitor_name','$visitor_num','$visitor_email','$visitor_Reason','$emp_name','$emp_Role',$emp_email');";
if ($con->query($sql)){
echo "true";
}else{
echo "false";
}
}
?>
AND THIS SOME MY TEXTFIELD CODE I DID THIS FOR ALL THE TEXT INPUTS visitor_name,visitor_num,visitor_email,visitor_Reason,emp_name,emp_email,emp_role,
children: [
TextField(
controller: txtVname,
decoration: InputDecoration(
errorText: _valvname
? 'Please fill the detail'
: null,
labelText: "Visitors Name",
labelStyle: const TextStyle(
color: Color(0xff6D6C6CFF))),
),
TextField(
controller: txtVnumber,
decoration: InputDecoration(
labelText: "Visitors Mobile Number",
labelStyle: const TextStyle(
color: const Color(0xff6D6C6CFF)),
errorText:
_valvnumber ? 'Can\'t be Empty' : null,
),
),
AND THE ERROR MESSAGES ARE when I click submit button this is the error message is showing I don't know where to look for the bugs.
E/flutter (13778): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter (13778): <br />
E/flutter (13778): ^
E/flutter (13778):
E/flutter (13778): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1405:5)
E/flutter (13778): #1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1272:9)
E/flutter (13778): #2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:937:22)
E/flutter (13778): #3 _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter (13778): #4 JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter (13778): #5 JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter (13778): #6 jsonDecode (dart:convert/json.dart:155:10)
E/flutter (13778): #7 _HomepageState.build.insertData (package:vms_1/Home_Page.dart:67:21)
E/flutter (13778): <asynchronous suspension>
E/flutter (13778):
AND THIS IS WHAT PHP SHOWS
Notice: Undefined index: visitor_name in /storage/ssd4/645/18700645/public_html/vsave.php on line 3
Notice: Undefined index: visitor_num in /storage/ssd4/645/18700645/public_html/vsave.php on line 4
Notice: Undefined index: visitor_email in /storage/ssd4/645/18700645/public_html/vsave.php on line 5
Notice: Undefined index: visitor_Reason in /storage/ssd4/645/18700645/public_html/vsave.php on line 6
Notice: Undefined index: emp_name in /storage/ssd4/645/18700645/public_html/vsave.php on line 7
Notice: Undefined index: emp_Role in /storage/ssd4/645/18700645/public_html/vsave.php on line 8
Notice: Undefined index: emp_email in /storage/ssd4/645/18700645/public_html/vsave.php on line 9
and this too
W/IInputConnectionWrapper( 8071): getTextBeforeCursor on inactive InputConnection
I/AssistStructure( 8071): Flattened final assist data: 388 bytes, containing 1 windows, 3 views
W/IInputConnectionWrapper( 8071): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper( 8071): getSelectedText on inactive InputConnection
Solution 1:[1]
Let's fix first your problem with your API. Please try to format your insertData() function similar to this:
Future insertData() async {
setState(() {
visible = true;
});
var url = "https://astatic-scab.000webhostapp.com/vsave.php";
var response = await http.post(
Uri.parse(url),
body: {
"visitor_name": txtVname.text.toString(),
"visitor_num": txtVnumber.text.toString(),
"visitor_email": txtemail.text.toString(),
"visitor_Reason": txtvReason.text.toString(),
"emp_name": txtename.text.toString(),
"emp_Role": txtRole.text.toString(),
"emp_email": txtemail.text.toString(),
},
);
var message = jsonDecode(res.body);
if (message == "true") {
print("Successful " + message);
} else {
print("Error: " + message);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kei Credo |
