Home > database >  How to add the path of my uploaded files in mysql database using PHP?
How to add the path of my uploaded files in mysql database using PHP?

Time:05-09

This is my upload.php :

    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);
            }
            $filename = "/" . $file["name"];
            $uploadfile = $uploadfile . $filename;
            /*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));
        }

And my function to add the files path in my db :

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

My PHP debug says that there is an error in the last line of my addpic function :

return $stmt->execute(array(":title"=>$user["title"],":description"=>$user["description"]));

But I don't understand what is the error because each field exist in my db ?

Do you have any idea why I have this error ?

Exception: PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /Applications/MAMP/htdocs/legogram/app/model/model.php:86
Stack trace:
#0 /Applications/MAMP/htdocs/legogram/app/model/model.php(86): PDOStatement->execute(Array)
#1 /Applications/MAMP/htdocs/legogram/app/controller/addphoto.php(48): Model->addpic(Array)
#2 /Applications/MAMP/htdocs/legogram/public/index.php(67): Addphoto->add()
#3 {main}

Thank you for your help

CodePudding user response:

In your declaration you only have the :path bind. And in the execute method you have two values, none of which correspond to the bind name.

The line with error should look like this:

return $stmt->execute(array(":path"=>$user["path"]));

CodePudding user response:

You need to set which fields you want to update, so for add new column you need to set something like this:

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

And if you want to update the existing column, you need something like this:

public function addpic($user) {
    $this->open();
    $stmt = $this->pdo->prepare("UPDATE pictures set path = :path WHERE title = :title AND description = :description");
    return $stmt->execute(array(":title"=>$user["title"],":description"=>$user["description"],":path"=>$path));
}
  • Related