i am making an album book in php for each user in php, but all pics are shown on page,, i want to show them according to user login,,not all pics
plz help,,,thanks

Dani AI

Generated

As described, seeing every picture usually means the listing code is not filtering by the logged-in user. Two clean patterns: store each image record with a user_id in the database (recommended), or put uploads in per-user folders and read only that folder. The key is: on upload save the owner, and on display select only rows/files that match the current session user.

Example (safe, DB-driven listing using PDO and session user):

session_start();
if (empty($_SESSION['user_id'])) { header('Location: login.php'); exit; }
$user_id = (int) $_SESSION['user_id'];

$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4','dbuser','dbpass',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$stmt = $pdo->prepare('SELECT filename, caption FROM photos WHERE user_id = ? ORDER BY created_at DESC');
$stmt->execute([$user_id]);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $src = 'uploads/' . htmlspecialchars($row['filename'], ENT_QUOTES, 'UTF-8');
  echo '<img src="' . $src . '" alt="' . htmlspecialchars($row['caption'], ENT_QUOTES, 'UTF-8') . '">';
}

Minimal upload flow (store ownership and use a safe filename):

$user_id = (int) $_SESSION['user_id'];
$ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
$name = bin2hex(random_bytes(8)) . '.' . $ext;
move_uploaded_file($_FILES['photo']['tmp_name'], __DIR__.'/uploads/'.$name);
$stmt = $pdo->prepare('INSERT INTO photos (user_id, filename, caption) VALUES (?, ?, ?)');
$stmt->execute([$user_id, $name, $_POST['caption'] ?? '']);

Troubleshooting tips: confirm session_start() runs before checks, var_dump($_SESSION['user_id']) to verify the logged id, and inspect the SELECT to ensure a WHERE user_id = ... clause exists. Security notes: validate uploaded MIME with finfo or getimagesize(), use prepared statements, avoid storing files with original names, and consider serving files through a PHP script that checks ownership if files are outside webroot. As suggested, posting the upload/listing snippets and DB schema will make pinpointing the issue easier; ’s pointer to user-specific-query examples is on the right track.

Recommended Answers

All 2 Replies

post some code

Check this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.