我的表单不会发布任何文字或图片

时间:2017-03-31 02:53:06

标签: php forms image post imgur

这是我的代码,起初我在foreach循环中对“postimg”(表单中的输入变量)有一个未定义的索引通知,我调试了我的代码并通过检查是否设置了postimg来消除通知现在我的帖子和图片都不会出现,我相信问题仍然在于每个循环中使用postimg,我只是不知道究竟是什么。

<?php 
include("connect.php"); 
include("check.php");
include("image.php");

$posts = "";
//$postimg = "";

                if (isset($_POST['post'])) {
                            //  $time = connect::query('SELECT posted_at FROM dry_posts WHERE id=:postid', array(':postid'=>$_GET['id']));
                        //$postimg = $_POST['postimg'];
                    //if(isset($_POST['postimg']))
                    //{

                            //  $id = connect::query('SELECT id FROM dry_posts WHERE id=:id', array(':id'=>$_GET['id']));


                        if ($_FILES['postimg']['size'] == 0) 
                        {

                                $postbody = $_POST['postbody'];
                                $loggedInUserId = check::isLoggedIn();
                                if (strlen($postbody) > 160 || strlen($postbody) < 1) 
                                {
                                    die('Incorrect length!');
                                }

                                connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0)', array(':postbody'=>$postbody));

                        } 
                        else 
                        {
                                //$postid = Post::createImgPost($_POST['postbody']);
                            if (strlen($postbody) > 160) {
                                die('Incorrect length!');
                            }
                            connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0)', array(':postbody'=>$postbody));
                            $postid = connect::query('SELECT id FROM dry_posts ORDER BY ID DESC LIMIT 1');
                            Image::uploadImage('postimg', "UPDATE dry_posts SET postimg=:postimg WHERE id=:postid", array(':postid'=>$postid));
                        }
                   // }
                }
                if (isset($_GET['postid'])) 
                {

                        //Post::likePost($_GET['postid']);
                    if (!connect::query('SELECT post_id FROM post_likes WHERE post_id=:postid', array(':postid'=>$_GET['postid']))) 
                    {
                           connect::query('UPDATE dry_posts SET likes=likes+1 WHERE id=:postid', array(':postid'=>$_GET['postid']));
                           connect::query('INSERT INTO post_likes VALUES (null, :postid)', array(':postid'=>$_GET['postid']));
                    } 
                    else 
                    {
                          connect::query('UPDATE dry_posts SET likes=likes-1 WHERE id=:postid', array(':postid'=>$_GET['postid']));
                          connect::query('DELETE FROM post_likes WHERE post_id=:postid', array(':postid'=>$_GET['postid']));
                    }
                }

                // $posts = Post::displayPosts();
                $dbposts = connect::query('SELECT * FROM dry_posts ORDER BY id DESC');
                $posts = "";
                if(isset($_POST['postimg'])){
                foreach($dbposts as $p) {
                        if (!connect::query('SELECT post_id FROM post_likes WHERE post_id=:postid', array(':postid'=>$p['id']))) {
                                $posts .="<img src='".$p['postimg']."'>".htmlspecialchars($p['body'])."
                                <form action='dry.php?postid=".$p['id']."' method='post'>
                                        <input type='submit' name='like' value='Like'>
                                        <span>".$p['likes']." likes</span>
                                </form>
                                <hr /></br />
                                ";
                        } else {
                                $posts .="<img src='".$p['postimg']."'>".htmlspecialchars($p['body'])."
                                <form action='dry.php?postid=".$p['id']."' method='post'>
                                        <input type='submit' name='unlike' value='Unlike'>
                                        <span>".$p['likes']." likes</span>
                                </form>
                                <hr /></br />
                                ";
                        }
                }
         }
?>

这是头文件“image.php”,我使用imgur上传图片,我不相信我在这里有任何错误,有什么建议吗?

<?php

    class Image
    {
        public static function uploadImage($query,$params)
        {
            $image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));
            $options = array('http'=>array(
                'method'=>"POST",
                'header'=>"Authorization: Bearer 813d9b0ee1b108a3383f6bd016dd9260873fa681\n".
                "Content-Type: application/x-www-form-urlencoded",
                'content'=>$image
            ));
            $context = stream_context_create($options);
            $imgurURL = "https://api.imgur.com/3/image";
            if ($_FILES['profileimg']['size'] > 10240000) {
                die('Image too big, must be 10MB or less!');
            }
            $response = file_get_contents($imgurURL, false, $context);
            $response = json_decode($response);

            $preparams = array($formname=>$response->$data->$link);
            $params = $preparams + $params;

            connect::query($query,$params);

        }

    }

?>

1 个答案:

答案 0 :(得分:0)

这里有很多内容可以通过,但我会告诉你,如果你在form ='post'后的表单标签中没有enctype =“multipart / form-data”,你的图片就不会交。