0

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..

Abrar Lasebai
  • 93
  • 1
  • 11
  • 1
    A 500 error is a generic error message and covers pretty much every single thing that can go wrong with a PHP script. Check your server error logs to find out the exact error message. – aynber Feb 09 '23 at 16:49
  • @aynber Thank you for your reply, but how can I check server error logs? if that in flutter debug mode it's not showing anything just the "internal server error" that's why I'm so confused :( – Abrar Lasebai Feb 09 '23 at 16:55
  • 1
    The server error logs are often found in the web server error log files. In linux systems, it's found in a folder inside of `/var/log`, often named `apache`, `apache2`, `httpd`, or `nginx`. Sometimes there's an `error_log` somewhere in your project directory, depending on how your webserver is set up. – aynber Feb 09 '23 at 17:02
  • @aynber still trying to get it but I think that I'm not good with php, can you help me more? I am using Windows – Abrar Lasebai Feb 09 '23 at 17:23

1 Answers1

1

first you can try to add for debug purpose :

`<?php
// see errors:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);`

and maybe close your file with "?> "

Eboo
  • 81
  • 3
  • internal server error has disappeared, I think that's good right? but still does not add the data to the database – Abrar Lasebai Feb 09 '23 at 17:15
  • I got this: " Cannot access the body fields of a Request without content-type "application/x-www-form-urlencoded". #0 Request.bodyFields (package:http/src/request.dart:121:7) – Abrar Lasebai Feb 09 '23 at 17:22
  • And this is your problem right there. In Postman the request is (probably) made with contet-type "application/x-www-form-urlencoded" but in your flutter code you wrote `headers: { 'Content-Type': 'application/json;charset=UTF-8',` – Mike Szyndel Feb 13 '23 at 12:49