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';
}
}