0

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 database in phpMyAdmin


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">&times;</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">&times;</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">&times;</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">&times;</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">&times;</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

Axis
  • 49
  • 1
  • 10
  • so you are getting your The system encountered an error message? – Your Common Sense Feb 21 '17 at 10:53
  • Nope, I was getting a 'Success' message rather an error message, but the Insert statement doesn't still works – Axis Feb 21 '17 at 11:01
  • It simply can't be. Either there is a successful insert or an error. Check your premises. – Your Common Sense Feb 21 '17 at 11:03
  • I already set the database `setAttribute()` in my PDO, and if you look at my PHP codes, the 'see all errors' are enabled, and I var dummped possible variables that are gonna be sent to the database – Axis Feb 21 '17 at 11:03
  • it means insert was all right – Your Common Sense Feb 21 '17 at 11:03
  • Check it then, When I hit the submit button, it is '**Success**' but when I saw my database, the table is empty. – Axis Feb 21 '17 at 11:04
  • Yeah, if the `$inserstmt` was success, then display success message. But if it fails, then submits an error message. It should print out an error message but it shows an success message. (Note that I intentionally named it as inser rather than insert. – Axis Feb 21 '17 at 11:06
  • make sure you are checking the correct database – Your Common Sense Feb 21 '17 at 11:06
  • Yes, I already checked the database name, and the table it is sending data to – Axis Feb 21 '17 at 11:07
  • You don't have to tell me that :) I am neither your teacher nor mother. And I don't care for your code at all. You need it all for yourself, not for me. Like I said, there is nothing complex in this case - either there is an insert or an error. You just have to decide for yourself, which one. In case you still need any advise from the community, you must create a [Minimal, Complete, and Verifiable example](https://phpdelusions.net/pdo/mcve) – Your Common Sense Feb 21 '17 at 11:10

0 Answers0