My form won't post any text or images

Here’s my code, at first I had an undefined index notice for “postimg” (the input variable in the form) in the foreach loop, I debugged my code and got rid of the notice by checking if postimg was set, however now my posts and images won’t show up, I believe the problem still lies with the use of postimg in the for each loop, I just don’t know what exactly it is.

[php]<?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 />
                            ";
                    }
            }
     }

?>

Here’s the header file,“image.php”, where I upload images using imgur, I don’t believe I have any errors here, any suggestions?

<?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); } } ?>[/php]

Sorry, but neither will anyone else seeing your post. You have posted code that contains absolutely no comments to let anyone know what it is supposed to be doing (which would also help when writing the code), has a fair number of lines of code commented out, logically makes no sense in most places, has mismatched and non-existent variables, doesn’t include the upload form so that anyone would know what the input data should be (the forms in this code have nothing to do with uploading a file, are for liking/disliking posts, and apparently aren’t even being output on the page), doesn’t include all the code that would be needed to reproduce the problem, isn’t validating the input data before using it, is mixing form processing code and the output on the page, and has exposed the imgur api key.

Adding an isset() around some variable(s) has probably only hidden the fact that the form isn’t submitting the variable(s). Doing something to hide the error didn’t fix the code, just hid the fact that an input the code is expecting doesn’t exist.

I recommend that instead of trying to make this code ‘work’, that you first define what input data you need/have from the form, then write and test the form processing code associated with uploading the file to the web server. Once you can successfully upload the file to the web server, you can add a call to code that will upload that file to imgur.

The form processing code should do the following -

  1. Detect that a post method form was submitted at all.

  2. Detect if the $_FILES and $_POST, if you are using $_POST data, array(s) are empty. This will detect if the total size of the post data exceeds the post_max_size setting. If there is no $_FILES or $_POST data, there will nothing for the rest of the code to use and none of the elements in those arrays will be set.

  3. Validate the input data. If you use an array to hold the validation messages, you can validate all the inputs at once.

  4. If there are no validation errors (the errors array is empty), use the input data.

  5. If there are validation errors (the errors array is not empty), display them (by outputting the content of the errors array) when you (re)display the upload form.

[php]uploadImage($query,$params)
[/php]

Should be taking in the $_FILES array as a parameter; “$formname” doesn’t exist to do anything with the file; and you should not be passing a query into the method either.

You have a lot of complexity and odd dependencies that can easily break. Section each piece out and see what it is doing.

Sponsor our Newsletter | Privacy Policy | Terms of Service