postimg和postid array的未定义索引

时间:2017-04-23 19:13:24

标签: php arrays image undefined-index

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

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

我已经尝试检查是否设置了postimg和postid以及它们是否为空,但是if 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);


        }

    }

?>

1 个答案:

答案 0 :(得分:2)

仔细查看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) ); 电话:

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
)

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

   if ( ! Modernizr.objectfit ) {
  $('.post__image-container').each(function () {
    var $container = $(this),
        imgUrl = $container.find('img').prop('src');
    if (imgUrl) {
      $container
        .css('backgroundImage', 'url(' + imgUrl + ')')
        .addClass('compat-object-fit');
    }  
  });
}