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 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的