I have created simple apis using pure php, and uploaded the project to 000webhost, Get request is working good but when try to add data to database with POST request it returns Internal server error, even when the api works good in Postman. after I added this, as Sir Eboo's answer:
`<?php
// see errors:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);`
Internal server error disappeared but still not adding the data to the database !!! I've tried to check all the questions about this problem but still can't find a solution. is that maybe because 000webhost block my post request?
The function in flutter:
@override
Future<void> addStudent(String name, String grade, String note) async {
final url = Uri.parse(
"https://mystudentsrating.000webhostapp.com/crud/add_student.php");
final http.Response response = await http.post(url,
headers: {
'Content-Type': 'application/json;charset=UTF-8',
'Charset': 'utf-8',
"Accept": "application/json",
"contentType": "application/json"
},
body: jsonEncode(
<String, dynamic>{
'studentname': name,
'studentgrade': grade,
'studentnote': note
},
));
final result = utf8.decode(response.bodyBytes);
final message = jsonDecode(result);
if (response.statusCode != 200) throw Exception(message['status']);
}
}
The Add Student file:
<?php
include '../connection.php';
// see errors:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
$studentName = filter_request('studentname');
$studentGrade = filter_request('studentgrade');
$studentNote = filter_request('studentnote');
// $studentstars = filter_request('studentstars');
$stmt = $con->prepare(
"INSERT INTO `students` (`student_name`, `student_grade`,`student_total_grades`, `student_note`, `student_stars`)
VALUES ( ?,?,?,?,?)");
$stmt->execute(array(
$studentName, $studentGrade,$studentGrade, $studentNote, 0
));
$count = $stmt->rowCount();
if ($count > 0)
echo json_encode(
array("status" => "Success"));
else echo json_encode(
array("status" => "Fail"));
hope you can help me..