【问题标题】:undefined index for postimg and postid array'spostimg 和 postid 数组的未定义索引
【发布时间】:2017-09-20 09:16:49
【问题描述】:

这是我的代码,我在定义 $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);


        }

    }

?>

【问题讨论】:

  • 函数调用中有 4 个参数,但函数定义中只有 3 个参数。

标签: php arrays image undefined-index


【解决方案1】:

仔细看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)
);

因此,您的参数应该作为 一个 数组传递:

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

接下来,$params 中没有 'postid' 键。你有':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
)

【讨论】:

  • 谢谢,但现在我收到了这个通知,注意:数组到字符串的转换
  • 我应该自己转换吗?
  • 这意味着您尝试在某个地方打印数组值。
猜你喜欢
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-15
  • 2011-08-15
  • 2013-11-03
  • 2015-08-07
相关资源
最近更新 更多