不知道为什么我有未定义的偏移通知


Cant find why Im having undefined offset notice

我有下面的代码在news-create.php文件中,我有一个表单插入数据到我的新闻表

一切都很好,除非我的图片多次上传,当我插入我的新闻。

我得到这个错误:

注意:未定义offset: 3 in $ext = substr($gb['name'][$i],-3);

注意:未定义offset: 3 in if(in_array($gb['type'][$i],$extPerm))

你能给我一些帮助吗?

这是我的:

在下面的代码之前,我将用户在插入新闻表单中写入的内容存储在$f数组中,并上传我的新闻的单个图像,然后我执行插入:

    $insNews = $pdo->prepare("INSERT INTO news (img, title, category, content) VALUES (?, ?, ?, ?)");
    $insNews->bindParam(1,$f['img']);
    $insNews->bindParam(2,$f['title']);
    $insNews->bindParam(3,$f['category']);
    $insNews->bindParam(4,$f['content']);               
    $insNews->execute();
    $insNews = $pdo->lastInsertId();

我得到我的lastInsertId,因为我也想创建一个图像画廊,我的当前新闻,所以我需要关联的新闻,我当前插入与我的画廊表,所以我认为这个解决方案,我认为它应该工作…

如果insert返回成功我将进行多次上传,并将当前id的图库插入到表图库中

if($insNews->rowCount() >=1){
    if($_FILES['gb']['tmp_name']){
        $count = count($_FILES['gb']['tmp_name']);
        $gb = $_FILES['gb'];
        $folder = '../galery/';
        $year = date('Y');
        $month = date('m');
        if(!file_exists($folder.$year)){
        mkdir($folder.$year,0755);
        }
        if(!file_exists($folder.$year.'/'.$month)){
        mkdir($folder.$year.'/'.$month,0755);
        }
        for($i=0;$i<=$count;$i++){
            $ext = substr($gb['name'][$i],-3);
            $name = $folder.$year.'/'.$month.'/'.$idlast.'-'.$i.time().'.'.$ext;
            $extPerm = array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif');
            if(in_array($gb['type'][$i],$extPerm)){
            uploadImage($gb['tmp_name'][$i], $name, '800', $folder);
            //and then I will do my insert into galery table here
            $insGal = $pdo->prepare("INSERT INTO gallery (img, id_news) VALUES (?, ?)");
            $insGal->bindParam(1,$name);
            $insGal->bindParam(2,$idlast);
            }
        }
    }
    if($f['status'] == '1'){
        echo 'Sucess inserting news';
    }
    else{
        echo 'Sucess intersing news, but you need to active it.';
    }

$i <= $count修改为$i < $count。当数组包含3个元素时,索引从0运行到2

除了@Barmar的回答。在我看来,你在循环中计算错误的数组。代码:

$count = count($_FILES['gb']['tmp_name']);//shouldn't this be just $_FILES['gb'] as that's what you're iterating over? 
$gb = $_FILES['gb'];  
//
 for($i=0;$i<$count;$i++){ //$count contains the length of $_FILES['gb']['tmp_name']