I am using Flutter for mobile app development. I need to make a POST request to a PHP file in order to save data in my MySQL server. However, I check the request status code it seems OK(200). But, the request fails. I think in my PHP file there is a problem because I am not into PHP. Can anyone tell me if the problem in my Flutter code or PHP.
PHP insert file
<?php
require 'connect.php';
$kayitNo =$_POST['kayit_no'];
$stajTuru=$_POST['staj_turu'];
$yas=$_POST['yas'];
$doktor=$_POST['klinik_egitici'];
$cinsiyet=$_POST['cinsiyet'];
$sikayet=$_POST['sikayet'];
$ayiriciTani=$_POST['ayirici_tani'];
$kesinTani=$_POST['kesin_tani'];
$tedaviYontemi=$_POST['tedavi_yontemi'];
$etkilesimTuru=$_POST['etkilesim_turu'];
$kapsam=$_POST['kapsam'];
$gerceklestigiOrtam=$_POST['ortam'];
$status=$_POST['form_status'];
$tarih=$_POST['tarih'];
echo $kayitNo;
$query="INSERT INTO form_table(kayit_no, staj_turu, yas, klinik_egitici, cinsiyet, sikayet, ayirici_tani,
kesin_tani, tedavi_yontemi, etkilesim_turu, kapsam, ortam, form_status ,tarih) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
$stmt =$conn->prepare($query);
$stmt->bind_param("ssssssssssssss",$kayitNo,$stajTuru,$yas,$doktor,$cinsiyet,$sikayet,$ayiriciTani,
$kesinTani,$tedaviYontemi,$etkilesimTuru,$kapsam,$gerceklestigiOrtam, $status,$tarih);
$stmt->execute();
$error = $conn->error();
if ( empty( $error ) ) {
http_response_code( 201 );
} else {
echo $error;
}
$stmt->close();
$conn->close();
?>
HTTP POST method(Flutter-Dart)
Future insertFormToDatabase(FormData formData) async{
var url = Uri.parse("http://10.0.2.2/flutter/insert.php");
var response = await http.post(url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body:jsonEncode(formData.toMap())
);
if (response.statusCode == 201) {
print("request succ.");
return true;
} else {
print(response.statusCode);
print("request failed.");
return false;
} }
Flutter DevTools Network result
Response
<br />
<b>Warning</b>: Undefined array key "kayit_no" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>6</b><br />
<br />
<b>Warning</b>: Undefined array key "staj_turu" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>7</b><br />
<br />
<b>Warning</b>: Undefined array key "yas" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>: Undefined array key "klinik_egitici" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>9</b><br />
<br />
<b>Warning</b>: Undefined array key "cinsiyet" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>10</b><br />
<br />
<b>Warning</b>: Undefined array key "sikayet" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>11</b><br />
<br />
<b>Warning</b>: Undefined array key "ayirici_tani" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>12</b><br />
<br />
<b>Warning</b>: Undefined array key "kesin_tani" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>13</b><br />
<br />
<b>Warning</b>: Undefined array key "tedavi_yontemi" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>14</b><br />
<br />
<b>Warning</b>: Undefined array key "etkilesim_turu" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>15</b><br />
<br />
<b>Warning</b>: Undefined array key "kapsam" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>: Undefined array key "ortam" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>: Undefined array key "form_status" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>18</b><br />
<br />
<b>Warning</b>: Undefined array key "tarih" in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>19</b><br />
<br />
<b>Fatal error</b>: Uncaught mysqli_sql_exception: Column 'kayit_no' cannot be null in C:\xampp\htdocs\flutter\insert.php:28
Stack trace:
#0 C:\xampp\htdocs\flutter\insert.php(28): mysqli_stmt->execute()
#1 {main}
thrown in <b>C:\xampp\htdocs\flutter\insert.php</b> on line <b>28</b><br />
CodePudding user response:
Am I missing it? It looks like you forgot to add $stmt->execute();
in your PHP code. It would go after $stmt->bind_param(...);
and before $stmt->close();
. You prepared the statement but never executed it. That could be why it's not going into the database.
To return a 201 on success, you could try this right after $stmt->execute();
:
$error = $conn->error();
if ( empty( $error ) ) {
http_response_code( 201 );
} else {
// However you want to handle an error. Maybe echo $error here?
}
CodePudding user response:
I solved this issue. The problem is in my HTTP Request
method in Dart. Since I am requesting a response I need to use the HTTP Request method. However, in the previous case, I was posting an incorrect format of the data. That's why the PHP file is not posting the request to MySQL. And also there were some problems with my PHP file too. Thanks to @stevish for helping me in fixing the issues in my PHP file and warning me about the sent data type could be incorrect. So, I re-wrote my insertFormToDatabase
method as the following.
Future insertFormToDatabase(FormData formData) async{
var url = Uri.parse("http://10.0.2.2/flutter/insert.php");
var headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', url);
request.bodyFields = {
"kayit_no":formData.getKayitNo(),
"staj_turu":formData.getStajTuru(),
"yas":formData.getYas(),
"klinik_egitici":formData.getDoktor(),
"cinsiyet":formData.getCinsiyet(),
"sikayet":formData.getSikayet(),
"ayirici_tani":formData.getAyiriciTani(),
"kesin_tani":formData.getKesinTani(),
"tedavi_yontemi":formData.getTedaviYontemi(),
"etkilesim_turu":formData.getEtkilesimTuru(),
"kapsam":formData.getKapsam(),
"ortam":formData.getOrtam(),
"form_status":formData.getStatus(),
"tarih":formData.getTarih(),
};
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
print("success");
print(await response.stream.bytesToString());
return true;
}
else {
print("failed");
print(response.reasonPhrase);
return false;
}
}