basically i made a page where the user can posts , after he posts it shows in the home page with others posts , i want to make a page that shows the posts that the user posted only
i made this interface with flutter
when i click at the posts page , i get a message shows Failed to load posts
even tho the user has posted before
class UserPostsPage extends StatefulWidget {
@override
State<UserPostsPage> createState() => _UserPostsPageState();
}
class _UserPostsPageState extends State<UserPostsPage> {
List userPosts = [];
@override
void initState() {
super.initState();
fetchUserPosts();
}
Future<void> fetchUserPosts() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? userId = prefs.getString('userid');
if (userId != null) {
var url = Uri.parse("$GETMYPOSTS?userId=$userId");
var response = await http.get(url, headers: {"Accept": "application/json"});
if (response.statusCode == 200) {
var jsonData = json.decode(response.body);
setState(() {
userPosts = jsonData;
});
} else {
print('Failed to load posts');
}
}
}
.....
and this is the php code :
<?php
include("../connect.php");
$userid = isset($_GET['userid']) ? $_GET['userid'] : null;
if (!$userid) {
http_response_code(400);
echo json_encode(['error' => 'User ID is required']);
exit;
}
$stmt = $con->prepare("SELECT * FROM post_table WHERE author = ?");
$stmt->bind_param("s", $userid);
$list = array();
if ($stmt->execute()) {
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$list[] = $row;
}
echo json_encode($list);
} else {
http_response_code(500);
echo json_encode(['error' => $stmt->error]);
}
$stmt->close();
?>
PS : there are two tables that im using
post_table : author
and user: userid
tbh i dont know where is the problem exactly