【发布时间】:2023-02-10 02:28:06
【问题描述】:
我使用纯 php 创建了简单的 api,并将项目上传到 000webhost,Get 请求运行良好,但是当尝试使用 POST 请求将数据添加到数据库时,它返回内部服务器错误,即使 api 在 Postman 中运行良好。 在我添加这个之后,作为 Eboo 爵士的回答:
`<?php
// see errors:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);`
内部服务器错误消失但仍未将数据添加到数据库中!!! 我已经尝试检查有关此问题的所有问题,但仍然找不到解决方案。 是因为 000webhost 阻止了我的发帖请求吗?
颤振中的函数:
@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']);
}
}
添加学生文件:
<?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"));
希望你能帮我..
【问题讨论】:
-
500 错误是一种通用错误消息,几乎涵盖了 PHP 脚本可能出错的每一件事。检查您的服务器错误日志以找出确切的错误消息。
-
@aynber 感谢您的回复,但我如何检查服务器错误日志?如果在 flutter 调试模式下,它只显示“内部服务器错误”,这就是我如此困惑的原因:(
-
服务器错误日志通常可以在 Web 服务器错误日志文件中找到。在 linux 系统中,它位于
/var/log内的文件夹中,通常命名为apache、apache2、httpd或nginx。有时在您的项目目录中的某处有一个error_log,这取决于您的网络服务器的设置方式。 -
@aynber 仍在尝试获取它,但我认为我不擅长 php,你能帮我更多吗?我正在使用 Windows