doufen9815 2017-09-01 20:53
浏览 82
已采纳

if语句失败时,为什么错误数组不返回?

I'm using the below code to insert a new post into the database.

It's working almost flawlessly, the only issue is that the return $this->errors[] = $image->getErrors(); is not being reported at all by php, even when I'm purposely uploading a .txt file when it's not allowed (only jpegs are).

Since $image->isUploaded return false if move_file_uploaded failed, the reports should get shown.

After the function is called, nothing is inserted into the database because the error is there, it's just not being reported.

But if I upload a correct jpeg image, the $db->commit() is successful and the $image->isUploaded() returns true and uploads the images to the server as there are no errors.

$public errors = [];

public function newPost($post_title, $post_category, $post_instructions, $post_instructionImages)
{

    $this->post_title             = $post_title;
    $this->post_category          = $post_category;
    $this->post_instructions      = $post_instructions;

    //this here just checks the $this reference to see if it's not empty
    $this->validate_post();

    if(empty($this->errors)){
        $db = static::getDB();

        $db->beginTransaction();
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sth = $db->prepare('INSERT INTO post (title, category ) VALUES (:title, :category)');
        $sth->bindValue(':title', $post_title, PDO::PARAM_STR );
        $sth->bindValue(':category', $post_category, PDO::PARAM_STR );
        $sth->execute();

        $postId = $db->lastInsertId();

        if(isset($post_instructions)){

            $sth = $db->prepare('INSERT into post_instructions (post_id, instructions) VALUES (:post_id, :instructions)');
            $sth->bindValue(':post_id', $postId, PDO::PARAM_INT );
            $sth->bindValue(':instructions', $data['instructions'], PDO::PARAM_STR );
            $sth->execute();

            $instructionsId = $db->lastInsertId();

            $uploadImage = new uploadImages();
            $image = $uploadImage->listen($post_instructionImages, 'uploads/post/img/instructions', 'instruction_');

            if($image->isUploaded()){
                $image_name = $image->getFileName();
                $sth = $db->prepare('UPDATE instructions SET image = :image WHERE id = :id');
                $sth->bindValue(':image', $image_name, PDO::PARAM_STR );
                $sth->bindValue(':id', $instructionsId, PDO::PARAM_INT);
                $sth->execute();
            } else {
                return $this->errors[] = $image->getErrors();
            }
        }

        return $db->commit();
    }

    return false;
}

This is how I'm calling the function:

if($this->post->newPost($post_title, $post_category, $post_instructions, $post_instructionImages)) {
    Flash::addMessage('New post added');
} else {
    print_r($this->post->errors);
}

What I've tried:

I've tried var_dump and print_r the $errors but not even then, nothing get's shown, not even inside the if statement above. I've been trying to understand why that is for literally the past 9 hours, I'm asking as a last resort. I didn't think it'd be necessary but here is the imageUpload class.

Update:

Here is the validate_post function

public function validate_post(){

    if($this->post_title == ''){
        $this->errors[] = 'Post title is required';
    }

    if($this->post_category == ''){
        $this->errors[] = 'Post category is required';
    }

    if($this->post_instructions == ''){
        $this->errors[] = 'Post instructions are required';
    }
}
  • 写回答

1条回答 默认 最新

  • drelgkxl93433 2017-09-01 21:13
    关注

    You have a logic problem:

    if($this->post->newPost(...)) {
        Flash::addMessage('New post added');
    } else {
        print_r($this->post->errors);
    }
    

    You're printing the error messages if the return value of the newPost() method evaluates to false.

    return $this->errors[] = $image->getErrors();
    

    This will append the result of $image->getErrors() to the $this->errors array and then return $this->errors.

    $this->errors at that point is an array. An array that contains values will always evaluate to true in PHP. That's why your if-statement does not display the error messages.

    To fix this, explicitly return false when something went wrong:

    $this->errors[] = $image->getErrors();
    return false;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么