在PHP中插入语法和图像上传

时间:2017-02-21 10:51:23

标签: php mysql pdo sql-insert

我脚本中的图片上传不起作用。但是,如果变量($imgName)为null,则查询应该仍然有效,因为我的 phpmyadmin 中的image列可以默认为 NULL 和NULL复选框已选中。所以,我的代码应该仍然有效。是的,代码被识别为“工作”,系统返回 NO 错误(参见下面的脚本)。

My database in phpMyAdmin

我的PHP代码(仅限):

<?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(Bootstrap格式有点长,所以我把它发布在Pastebin上

更新
我对标题感到抱歉,它应该是 INSERT 而不是更新

0 个答案:

没有答案