The image uploading in my scripts doesn't works. However, if the variable ($imgName
) is null, the query should still works because the image column in my phpmyadmin is can be default to NULL and the NULL checkbox is checked. So, my codes should still works. Yeah, the codes is recognized as 'working' and there is NO errors returned by the system (see the script below).
My PHP Codes (only):
<?php
session_start();
require_once '../assets/php/db.conn.php';
require_once '../assets/php/func.main.php';
date_default_timezone_set('Asia/Manila');
if(!isset($_SESSION)){
header('Location: ../');
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
$_targetdir = "../assets/images/";
$imgErrors = array();
if($_POST){
$title = strip_tags(trim($_POST['title']));
$content = strip_tags(trim($_POST['content']));
$articleid = genRand();
$datetime = date('Y-m-d H:i:s');
$imgName = null;
if(empty($title)){
$msgs = '
<div class="alert alert-warning alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Warning!</strong> Please add a title to your article!
</div>
';
} elseif(strlen($title) > 255){
$msgs = '
<div class="alert alert-warning alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Warning!</strong> Your title is too long! The maximum length is only 255 characters!
</div>
';
} elseif(empty($content)){
$msgs = '
<div class="alert alert-warning alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Warning!</strong> Please insert the content of your article!
</div>
';
}
if(isset($_POST['imgUpload'])){
$_targetfile = $_targetdir . basename($_FILES['imgUpload']["name"]);
$filetype = pathinfo($_targetfile, PATHINFO_EXTENSION);
$check = getimagesize($_FILES["imgUpload"]["tmp_name"]);
if($check == false){
array_push($imgErrors, 'File is not an image!');
}
if(file_exists($_targetfile)){
array_push($imgErrors, 'Image already exists!');
}
if ($_FILES["imgUpload"]["size"] > 500000) {
array_push($imgErrors, 'Image filesize is too big!');
}
if($filetype != "jpg" && $filetype != "png" && $filetype != "jpeg" && $filetype != "gif" ) {
array_push($imgErrors, 'Sorry, your image file type is not supported!');
}
array_filter($imgErrors);
if(empty($imgErrors)){
if(move_uploaded_file($_FILES['imgUpload']['tmp_name'], $_targetfile)){
$imgName = $_FILES["imgUpload"]["name"];
}
}
}
# var_dump() every possible variables, still getting the results I want
var_dump($articleid);
var_dump($title);
var_dump($imgName);
var_dump($content);
var_dump($datetime);
#$sql = "INSERT INTO `posts` (`articleid`, `title`, `image`, `content`, `created_at`) VALUES (?,?,?,?,?)";
#$insertstmt = $conn->prepare($sql);
#$insertstmt->execute(array($articleid, $title, $imgName, $content, $datetime));
#var_dump($insertstmt);
$insertsql = "INSERT INTO `posts` (`articleid`, `title`, `image`, `content`, `created_at`) VALUES (:AID, :TLT, :IMG, :CNT, :TM)";
$inserstmt = $conn->prepare($insertsql);
$inserstmt->bindParam(':AID', $articleid);
$inserstmt->bindParam(':TLT',$title);
$inserstmt->bindparam('IMG',$imgName);
$inserstmt->bindParam(':CNT',$content);
$inserstmt->bindParam(':TM', $timestamp);
if($inserstmt){
$msgs = '
<div class="alert alert-success alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> Your article was posted successfully!
</div>
';
} else {
$msgs = '
<div class="alert alert-danger alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error!</strong> The system encountered an error, please try again later!
</div>
';
}
}
?>
All of my codes are here on this Pastebin (the Bootstrap formatting was kinda long, so I posted it on Pastebin
UPDATE:
I'm sorry about the title, it should be an INSERT instead of an UPDATE