无法找到我的表单问题而不发布任何文本或图像,我的帖子方法没有设置

时间:2017-04-05 20:14:59

标签: php forms image post imgur

这是我的代码,起初我在foreach循环中对“postimg”(表单中的输入变量)有一个未定义的索引通知,我通过检查是否设置了postimg来摆脱通知,但现在我的帖子和在调试我的代码之后,图像不会显示出来我相信问题仍然在于每个循环中使用postimg,我只是不知道究竟是什么。检查邮件是否设置后,不会执行任何代码。

<?php 
include("connect.php"); 
include("check.php");
include("image.php");
$postbody = "";
$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'];
                    echo 'helloooooooooooooooooooooo';

                   // 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 />
                                ";
                        }
                }
      }      

    <form action="try.php" method="post" class = "forum" enctype="multipart/form-data">
        <textarea name="postbody" rows="4" cols="60" class = "text"></textarea>
         <br />Upload an image:
        <input type="file" name="postimg">
        <input type="submit" name="post" value="Post">
</form>
?>

这是头文件“image.php”,我使用imgur上传图片,我修复过的任何错误,有什么建议吗?

<?php
include_once("connect.php");
    class Image
    {
        public static function uploadImage($query,$params)
        {
            if(isset($_FILES['formname']))
            {
                $formname = "";
            $image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));
            $options = array('http'=>array(
                'method'=>"POST",
                'header'=>"Authorization: Bearer 8cd9d1bb71401737dc720f9381d8e9763f580af6\n".
                "Content-Type: application/x-www-form-urlencoded",
                'content'=>$image
            ));
            $context = stream_context_create($options);
            $imgurURL = "https://api.imgur.com/3/image";
            if ($_FILES[$formname]['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);

            }
        }

    }

?>

0 个答案:

没有答案