postimg 和 postid 数组的未定义索引

undefined index for postimg and postid array's

这是我的代码,在我定义 $params 变量的倒数第二行有未定义的索引通知。这是我对 uploadImage 的调用,我从另一个文件

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

我已经尝试检查是否设置了 postimg 和 postid 以及它们是否为空,但是这些 if 语句中没有任何内容被执行。

<?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);


        }

    }

?>

仔细观察 Image::uploadImage 调用:

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)
);

因此,您的参数应作为 one 数组传递:

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

接下来,您没有 'postid' 键入 $params。你有 ':postid'.

因此,任一行

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

必须是:

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

或者,传递给函数的参数应该是:

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