Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未定义的属性:stdClass::$id错误_Php_Mysql_Laravel - Fatal编程技术网

Php 未定义的属性:stdClass::$id错误

Php 未定义的属性:stdClass::$id错误,php,mysql,laravel,Php,Mysql,Laravel,每当我上传一张新图片时,我试图将imagename和imagepath记录添加到mysql数据库中,但是,我遇到以下错误: undefined property: stdClass::$id in C:\Service\ChallengeService.php on line 184 undefined property: stdClass::$id in C:\Service\ChallengeService.php on line 188 我复制了一个函数,它也以同样的方式将图像保存到类似

每当我上传一张新图片时,我试图将imagename和imagepath记录添加到mysql数据库中,但是,我遇到以下错误:

undefined property: stdClass::$id in C:\Service\ChallengeService.php on line 184
undefined property: stdClass::$id in C:\Service\ChallengeService.php on line 188
我复制了一个函数,它也以同样的方式将图像保存到类似的字段中,我几乎只是重用了这个函数,所以我不知道为什么它会给我一个错误

这是更新我的数据库的功能:

public function updateChallengeImage($challenge) {
        try {
            $query = "update challenges
                            set image = :image
                                ,image_file_name = :image_file_name
                      where      
                        id = :id";
            $values = array(
                ":image" => $challenge->image,
                ":image_file_name" => $challenge->image_file_name,
                ":id" => $challenge->id
            );
            $stmt = $this->db->prepare($query);
            $stmt->execute($values);
            return $challenge->id;
        } catch (PDOException $e) {
            $this->logger->error("PDO: ".$e->getMessage(), ["class"=>get_class($this),"method"=>__METHOD__]);
            return -1;
        }
    }
为什么没有传递$id变量?我的日志中也出现了以下警告:

PHP:WARNING Creating default object from empty value in `C:\\classes\napify\Controller\ChallengesController.php on line 106`
但是,我的控制器函数看起来不错,它应该从数据库加载ID:

public function saveChallengeImage($request, $response, $args) {
        $files = $request->getUploadedFiles();
        $data = $request->getParsedBody();
        $valid = true;
        if (!(Utils::isImage($files['image']->file))) {
            $this->flash->addMessage('message', 'Las imagenes no deben superar los 700kb ');
            $this->flash->addMessage('error', 1);
            $valid = false;
        }
        if ($valid) {
            $challenge = $this->ChallengeService->getChallenge(filter_var($data['challenge_id'], FILTER_SANITIZE_STRING));
            $challenge->image_file_name = $files['image']->getClientFileName();
            $challenge->image = $this->saveFile($files['image']);
            $this->ChallengeService->updateChallengeImage($challenge);
            $this->flash->addMessage('message', 'Se guardó la imagen.');
        }
正在控制器上调用的getChallenge函数

 public function getChallenge($id) {
        try {
            $query = "select
                         c.id
                        ,c.title
                        ,c.description
                        ,c.active
                        ,c.start_date
                        ,c.end_date
                        ,c.image
                        ,c.image_file_name
                        ,c.color1
                        ,c.color2
                        ,c.color3
                        ,c.color4
                    from 
                        challenges c
                    where
                        id = :id";
            $values = array(
                ":id" => $id
            );
            $stmt = $this->db->prepare($query);
            $stmt->execute($values);
            $stmt->setFetchMode(PDO::FETCH_CLASS, 'napify\Model\Challenge');
            return $stmt->fetch();
        } catch (PDOException $e) {
            $this->logger->error("PDO: ".$e->getMessage(), ["class"=>get_class($this),"method"=>__METHOD__]);
            return null;
        }
    }

我不确定为什么它无法从数据库加载ID,或者无法使用imagefile和imagename值更新数据库。任何能为我指明正确方向或能告诉我哪里出错的帮助都将不胜感激

如果在saveChallengeImage函数中打印$data['challenge\u id',是否打印正确的质询id值?
$this->ChallengeService->getChallenge(…)
不会为
$challenge
变量返回任何内容。这就是为什么
$challenge->id
未定义的原因。