2

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';
    }
}
8
  • Can you add the validate_post method? Commented Sep 1, 2017 at 20:55
  • I think you just need to check the return value when calling the function. I.e. if(true===$this->post .... Otherwise the error array will be type juggled to true. Commented Sep 1, 2017 at 20:59
  • @JonStirling I've added it. Commented Sep 1, 2017 at 21:00
  • @jh1711 I'll give it a go. Commented Sep 1, 2017 at 21:01
  • $image = $uploadImage->listen( then you have $image->getErrors(); so can you show us what the ->listen function does? Commented Sep 1, 2017 at 21:01

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

that's the conclusion I was starting to come (as you can see in my previous comment to Martin before you posted this) because I was thinking that $this->errors[] doesn't actually return false but just a set of data in an array. It would have taken me awhile to figure this out further though, I appreciate you taking your time to give me a huge hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.