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 />
source https://stackoverflow.com/questions/71269483/flutter-http-post-request-to-a-php-file
No comments:
Post a Comment