route problem
am building a facebook clone using reactJs with php in back-end
i have this small problem in routing where i wanted to check other users profile it dont take me to their profiles
it just take me to my profile instead and in my url it show undefined
1 Reply
<Route path="/profile/:id" element={ <Layout><Profile /></Layout>} />
<Route path="/profile/:id" element={ <Layout><Profile /></Layout>} />
{/* Results dropdown */}
{results.length > 0 && (
<div className="absolute top-14 left-10 bg-white shadow-lg rounded-md p-2 w-64">
{results.map((user, i) => (
<div key={i} className="flex items-center gap-2 p-2 hover:bg-gray-100 rounded">
<img
src={user.profile_pic}
alt={`${user.first_Name} ${user.last_Name}`}
className="w-8 h-8 rounded-full"
/>
<Link to={`/profile/${user.id}`}>
<span>{user.first_Name} {user.last_Name}</span>
</Link>
{console.log("User data:", user)} {/* Debug log */}
</div>
))}
</div>
)}
{/* Results dropdown */}
{results.length > 0 && (
<div className="absolute top-14 left-10 bg-white shadow-lg rounded-md p-2 w-64">
{results.map((user, i) => (
<div key={i} className="flex items-center gap-2 p-2 hover:bg-gray-100 rounded">
<img
src={user.profile_pic}
alt={`${user.first_Name} ${user.last_Name}`}
className="w-8 h-8 rounded-full"
/>
<Link to={`/profile/${user.id}`}>
<span>{user.first_Name} {user.last_Name}</span>
</Link>
{console.log("User data:", user)} {/* Debug log */}
</div>
))}
</div>
)}
<?php
session_start();
header("Content-Type: application/json");
if (!isset($_SESSION['user_id'])) {
echo json_encode(["success" => false, "message" => "Not logged in"]);
exit;
}
require_once "conn.php";
// If ?id is provided, use it, otherwise default to the logged-in user
$id = $_GET['id'] ?? $_SESSION['user_id'];
$stmt = $pdo->prepare("SELECT r.id, r.first_name, r.last_name, r.email, r.profile_pic,
p.bio, p.location, p.birthday, p.cover_photo
FROM register r
LEFT JOIN profiles p ON r.id = p.user_id
WHERE r.id = :id");
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
echo json_encode(["success" => true, "user" => $user]);
} else {
echo json_encode(["success" => false, "message" => "User not found"]);
}
<?php
session_start();
header("Content-Type: application/json");
if (!isset($_SESSION['user_id'])) {
echo json_encode(["success" => false, "message" => "Not logged in"]);
exit;
}
require_once "conn.php";
// If ?id is provided, use it, otherwise default to the logged-in user
$id = $_GET['id'] ?? $_SESSION['user_id'];
$stmt = $pdo->prepare("SELECT r.id, r.first_name, r.last_name, r.email, r.profile_pic,
p.bio, p.location, p.birthday, p.cover_photo
FROM register r
LEFT JOIN profiles p ON r.id = p.user_id
WHERE r.id = :id");
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
echo json_encode(["success" => true, "user" => $user]);
} else {
echo json_encode(["success" => false, "message" => "User not found"]);
}
const { id } = useParams();
useEffect(() => {
fetch(`/api/profiles.php?id=${id}`, {
method: "GET",
credentials: "include", // ✅ important to send session cookie
})
.then((res) => res.json())
.then((data) => {
if (data.success) {
setUser(data.user);
}
});
}, []);
const { id } = useParams();
useEffect(() => {
fetch(`/api/profiles.php?id=${id}`, {
method: "GET",
credentials: "include", // ✅ important to send session cookie
})
.then((res) => res.json())
.then((data) => {
if (data.success) {
setUser(data.user);
}
});
}, []);