How i can choose the catalog of photos for the current user?

I’m working on an app with flutter (API),and wondering how I can select pictures from catalog number 1, for example, but only for the current user.
It means that I want to replace the number 11 with the current user ID …or another way .

Note : “user_id” in photos table = “id” in users table

photos table:

  <?php
    include "connect.php" ;
    $sql  = "SELECT * FROM photos WHERE user_id = 11 AND photo_cata = 1 " ;
     $stmt = $con->prepare($sql);
     $stmt->execute(array()) ;
     $photos = $stmt->fetchAll(PDO::FETCH_ASSOC) ;
     echo json_encode($photos) ;
     ?>

login php

 <?php
include "connect.php";
if ($_SERVER['REQUEST_METHOD'] == "POST"){

  $email    = filter_var( $_POST['email'] , FILTER_SANITIZE_EMAIL) ;
  $password =  $_POST['password'] ;
  $token = $_POST['token'] ;

  $stmt = $con->prepare("SELECT * FROM users WHERE email = ? AND password = ?") ;
  $stmt->execute(array($email , $password));

  $user = $stmt->fetch() ;

   $row = $stmt->rowcount()  ;

   if ($row > 0) {

       $id        = $user['id'] ;
       $stmt2 = $con->prepare("UPDATE users SET token = ? WHERE id = ? ") ;
       $stmt2->execute(array($token , $id )) ;


       $username  = $user['username'] ;
       $email     = $user['email'] ;
       $password  = $user['password'] ;
       echo json_encode(array('id' => $id , 'username' => $username ,'email' => $email ,'password' => $password , 'status' => "success"));
   }else {
     echo json_encode (array('status' => "faild" , 'email' => $email  , 'password' => $password) );
 }


}
?>

Well in your login page, when you have determined that a user is successfully logged in, what you typically do is start up a session and put the user details into it. This includes their user ID (the current user ID).

Then on your user photos page, you again start the session (lookup session_start and how to use sessions in PHP if you don’t know already) and in that page you pull out the user ID and feed it into your SQL statement, replacing the “11” with that ID… typically with a placeholder (either a named one like :user_id or generic like ?.


session_start();

$current_user_id = $_SESSION['user_id'];  //<--- Create during successful login

$sql  = "SELECT * FROM photos WHERE user_id = :user_id AND photo_cata = :photo_category_id" ;
$stmt = $con->prepare($sql);

// Use your current user id and some photo gallery id to find photos
// Notice we use the named parameters to feed the variables into the query
$stmt->execute(array(":user_id" => $current_user_id, ":photo_category_id" => $photo_gallery_id)) ;
$photos = $stmt->fetchAll(PDO::FETCH_ASSOC) ;

Again you could also use the generic placeholder values like you did in login.php with the question marks. But the goal here is to stash the user’s ID into their session and then any page you want to read in the current user ID, you can pull it out of the session and use it in your queries.

I hope you get the idea. This is pretty standard, so you can find tons of info on the Internet about how to do this if you are still unclear. :slight_smile:

1 Like

This actually looks like a junction table. Wouldn’t it be better to put user_age in the users table? Then have just this junction table have the columns; photo_id, user_id, and photo_cata.

photo_id would point to the photos table which would have the appropriate columns associated with a photo.

user_id would point to the ID of the current user or specified user which then you can specify in the selection that you want to grab the user’s age and other various columns.

photo_cata, I’m assuming the relates to the photo catalog or a gallery of some sort.

OP should not be storing users age at all. What should be stored is the users birth-date and then calculate the age as needed. If the intent is for users age at time of photo, then you would use date photo taken calculated against birth-date.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.