Search code examples
phparraysimageundefined-index

undefined index for postimg and postid array's


Here's my code, I have undefined index notices on the second to last line where I define the $params variable. Here is my call to uploadImage where I pass $params value from another file,

Image::uploadImage('postimg', "UPDATE dry_posts SET postimg = :postimg WHERE id = :postid", array(':postid' => $postid),array(':postimg' => $postimg));

I've tried checking if postimg and postid is set and if they were empty, but nothing in those if statements were being executed.

<?php
include_once("connect.php");

    class Image
    {
        public static function uploadImage($formname,$query,$params)
        {
            $image = "";

            $image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));

            $options = array('http'=>array(
                'method'=>"POST",
                'header'=>"Authorization: Bearer access code here\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!');
            }
                  $curl_handle=curl_init();
                  curl_setopt($curl_handle,       CURLOPT_URL,'https://api.imgur.com/3/image&mpaction=convert format=flv');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'beautify');
$response = curl_exec($curl_handle);
curl_close($curl_handle);

                echo 'hell0';

            $response = json_decode($response);
            $params = array(':postid' => $params['postid'], ':postimg' => $params['postimg']);
            connect::query($query,$params);


        }

    }

?>

Solution

  • Look closer at Image::uploadImage call:

    Image::uploadImage(
        // $formname argument
        'postimg', 
        // $query argument 
        "UPDATE dry_posts SET postimg = :postimg WHERE id = :postid",  
        // $params argument
        array(':postid' => $postid),
        // Wait what is this?
        array(':postimg' => $postimg)
    );
    

    So, your params should be passed as one array:

    Image::uploadImage(
        // $formname argument
        'postimg', 
        // $query argument 
        "UPDATE dry_posts SET postimg = :postimg WHERE id = :postid",  
        // $params argument
        array(
            ':postid' => $postid,
            ':postimg' => $postimg
        )
    );
    

    Next, you don't have 'postid' key in $params. You have ':postid'.

    So, either line

    $params = array(':postid' => $params['postid'], ':postimg' => $params['postimg']);
    

    must be:

    // add ':' prefix
    $params = array(':postid' => $params[':postid'], ':postimg' => $params[':postimg']);
    

    Or, argument passed to function should be:

    // $params argument, no `:` prefixes
    array(
        'postid' => $postid,
        'postimg' => $postimg
    )