bkbn
bkbn

Reputation: 41

How to retrieve the ID of my current user in PHP to put it in another mysql table?

I have this function in my controller and I want to retrieve the ID of the current logged user to put it in my "picture" mysql table to associate the image to a user.

public function add()
    {
        $file = $_FILES["image"];
        if (!(isset($file))) {
            $error = "Fichier manquant";
        } else if ($file["size"] == 0) {
            $error = "Fichier vide ou trop grand (max 3MB)";
        } else if ($file["error"] != 0) {
            $error = "Problème lors de l'envoi du fichier";
        } else {
            // chaque utilisateur possède son propre répertoire d’upload
            $uploadfile = $GLOBALS["upload"] . "/" . $_SESSION["login"];
            /*echo $uploadfile . "<br>";*/
            if (!file_exists($uploadfile)) {
                mkdir($uploadfile, 0777, true);
            }
            global $owner_id; 
            $filename = "/" . $file["name"];
            $uploadfile = $uploadfile . $filename;
            //$owner_id=$this->model->loadId($login);
            /*echo $uploadfile  . "<br>";
            echo $file['tmp_name']  . "<br>";*/
            print_r($_FILES);
            print_r($_REQUEST);
            move_uploaded_file($file['tmp_name'], $uploadfile);
            $this->model->addpic(array("path" => $GLOBALS["uploadURL"] . "/" . $_SESSION["login"] . $filename));
            $this->model->addpic(array("owner_id" => $_GET['id']));
        }

        require "../app/view/_templates/header.php";
        require "../app/view/profile.php";
        require "../app/view/_templates/footer.php";
    }

In my model I have this function :

    public function addpic($user) {
        $this->open();
        $stmt = $this->pdo->prepare("INSERT INTO pictures (path, owner_id) VALUES (:path, :owner_id)");
        return $stmt->execute(array(":path"=>$user["path"], ":owner_id"=>$user["owner_id"]));
    }

and so with :owner_id"=>$user["owner_id"] I am trying to retrieve the User ID to put it in my table row but it doesn't work. I don't see how can I retrieve the ID from the User table of the database and put it my into the "Owner_ID" row of my "picture" database table.

Any idea ?

Thank you very much and have a nice day :)

Upvotes: 1

Views: 40

Answers (1)

user8034901
user8034901

Reputation:

You call ->addpic() twice, each one with an array with only one element as parameter. The method requires to have an array with two parameters though. Change

$this->model->addpic(array("path" => $GLOBALS["uploadURL"] . "/" . $_SESSION["login"] . $filename));
$this->model->addpic(array("owner_id" => $_GET['id']));

to

$this->model->addpic(array(
    "path" => $GLOBALS["uploadURL"] . "/" . $_SESSION["login"] . $filename,
    "owner_id" => $_GET['id']
));

Upvotes: 1

Related Questions