I need some help, i have back end file and using localhost to develop mobile app on flutter. The problem now when debugging the code, i get internal server error 505 from the back end. Let me share the code below both back end and front end. The flutter must supposed to be able to fetch the records from the database using api call, but to some strange reason i dont know maybe the path suppose not have folder name spaces in between. Please advice so can fix the error and continue, thanks.
// back end api file.
<?php
/****
@author:Gcobani Mkontwana
@date:13/09/2023
@This application uses cors for allowing application to run.
@This app sign up users and sign them to the database table.
*/
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json; charset=UTF-8");
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "agile_logistix_mobile";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Handle POST data
$data = json_decode(file_get_contents("php://input"));
//checking for empty username.
if (!empty($data->username)) {
$username = $data->username;
// Continue with other fields and database insertion
} else {
http_response_code(400); // Bad Request
echo json_encode(array("error" => "Username is required."));
}
// checking for email when empty.
if(!empty($data->email)) {
$email = $data->email;
}else {
http_response_code(400);
echo json_encode(array("error" => "Email is required"));
}
// checking for password when empty.
if(!empty($data->password)) {
$password = $data->password;
}else {
http_response_code(400);
echo json_encode(array("error" => "Password is required"));
}
// Perform the database query (replace 'users_registration' with your table name)
$stmt = $conn->prepare("INSERT INTO users_registration (username, email, password) VALUES (:username, :email, :password)");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $password);
if ($stmt->execute()) {
http_response_code(201);
echo json_encode(array("message" => "Registration successful"));
} else {
http_response_code(500);
echo json_encode(array("error" => "Registration failed"));
}
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(array("error" => "Database error: " . $e->getMessage()));
}
?>
// front end flutter app
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class RegistrationScreen extends StatefulWidget {
@override
_RegistrationScreenState createState() => _RegistrationScreenState();
}
class _RegistrationScreenState extends State<RegistrationScreen> {
final _formKey = GlobalKey<FormState>();
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
// form fields
String? _usernameError;
String? _emailError;
String? _passwordError;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Registration Page'),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Form(
key: _formKey, // Assign the key to the Form
child: Column(
children: <Widget>[
Container(
width: 265,
child: TextFormField(
controller: _usernameController,
decoration: InputDecoration(
labelText: 'Username',
prefixIcon: Icon(Icons.person),
errorText: _usernameError,
errorStyle:
TextStyle(color: Colors.red), // Set error text color
),
validator: (value) {
if (value == null || value.isEmpty) {
setState(() {
_usernameError = "Please enter a username";
});
return "Please enter a username";
}
setState(() {
_usernameError = null;
});
return null;
},
),
),
Container(
width: 265,
child: TextFormField(
controller: _emailController,
decoration: InputDecoration(
labelText: 'Email',
prefixIcon: Icon(Icons.email),
errorText: _emailError,
errorStyle:
TextStyle(color: Colors.red), // Set error text color
),
validator: (value) {
if (value == null || value.isEmpty) {
setState(() {
_emailError = "Please enter your email address";
});
return "Please enter your email address";
}
setState(() {
_emailError = null;
});
return null;
},
),
),
Container(
width: 265,
child: TextFormField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
prefixIcon: Icon(Icons.lock),
errorText: _passwordError,
errorStyle:
TextStyle(color: Colors.red), // Set error text color
),
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
setState(() {
_passwordError = "Please enter a password";
});
return "Please enter a password";
}
setState(() {
_passwordError = null;
});
return null;
},
),
),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_registerUser();
}
},
child: Text('Register'),
),
],
),
),
),
);
}
// logic to register user handled by back end api call.
void _registerUser() async {
final apiUrl = 'http://localhost:8080/Agile/Agile logistix/sign_up.php';
final username = _usernameController.text;
final email = _emailController.text;
final password = _passwordController.text;
//response back from the server.
final response = await http.post(Uri.parse(apiUrl), body: {
'username': username,
'password': password,
'email': email,
});
}
}