Codeigniter如果id = 1,它可以进行多个图像更新,但是id = 2它不起作用

时间:2014-11-25 05:57:17

标签: php codeigniter

我的代码多个图片上传和更新mysql数据库但是如果id = 1就会出现一个问题它正在进行多个图片上传和更新。这是不可行的白页。

表2

musteri_soru和musteri_cevap

正在更新冒号resim中的musteri_cevap

控制器代码:

function duzenle($no)
{
    if($_POST)
    {
            $arr1['baslik'] = $this->input->post('soru');
        $this->form_duzenle_model->duzenle($no,$arr1); 

    if($_FILES){

            $dizin=     "../upload/form_cevap/";
            $dosya_sayi=count($_FILES['cevap']['name']);

            for($i=0;$i<=$dosya_sayi;$i++){
            $isim= md5(uniqid(rand()));
            if(!empty($_FILES['cevap']['name'][$i])){
            move_uploaded_file($_FILES['cevap']['tmp_name'][$i],"./$dizin/$isim{$_FILES['cevap']['name'][$i]}");
            $arr['resim']=       $dizin.$isim.$_FILES['cevap']['name'][$i]; 
            }   
            $approve[] = $arr['resim'];   
            $it = $approve;
            print_r($approve);

            foreach($it as $n => $c):

            /* $deneme = $this->form_duzenle_model->cevapDuzenle($n,$c); */
            endforeach;
            }
    }

        redirect('form_duzenle/', 'refresh'); 
    }else{
        $this->bc->addCrumb('Düzenle');
        $veri   = $this->form_duzenle_model->form_duzenleGetir($no)->row();
        $veri2  = $this->form_duzenle_model->cevapListe($no)->result();
        $data   = array(
            'baslik'=>$veri->baslik,
            'veri'  =>$veri,
            'cevap' =>$veri2
            );
        $this->bc->addCrumb($veri->baslik,'form_duzenle/duzenle/'.$veri->no);
        $this->layout->view('form_duzenle/form_duzenle_duzenle',$data);
    }
}

模型代码:

 function duzenle($no,$data)
{
    $this->db->update($this->tablo,$data, array('no' => $no));
}

function cevapDuzenle($n,$dat)
{
    $data   = array('resim' => $dat);
    $this->db->update($this->ctablo,$data, array('soru_no' => $n));
}

我的桌子

enter link description here

3 个答案:

答案 0 :(得分:0)

检查你的for循环,

$dosya_sayi=count($_FILES['cevap']['name']);

for($i=0;$i<=$dosya_sayi;$i++)

for循环条件应该是$ i&lt; $ dosya_sayi作为数组索引始终从0开始。 所以正确的循环是

for($i=0;$i<$dosya_sayi;$i++)

答案 1 :(得分:0)

说实话,我不太确定您的上传是如何工作的,因为$_FILES数组不应该按照您上面显示的方式嵌套上传。

我不是说这肯定会奏效,但应该这样做:

function duzenle($no)
{

    //I would possible look at using the Form_validation Library here
    if (empty($_POST)) {
        $arr1['baslik'] = $this->input->post('soru');
        $this->form_duzenle_model->duzenle($no, $arr1);

        if (!empty($_FILES)) {
            $dizin = "../upload/form_cevap/";

            foreach ($_FILES as $name => $file) {

                //If there is an error there isn't any reason to try and upload this file
                if ($file['error'] !== 0) {
                    continue;
                }

                $name = $file['name'];
                $isim = md5(uniqid(rand()));
                move_uploaded_file($file['tmp_name'], "./$dizin/$isim$name");
                $arr['resim'] = $dizin . $isim . $name;

                //Not sure what's going on here so I haven't changed it
                $approve[] = $arr['resim'];
                $it = $approve;
                print_r($approve);

                foreach ($it as $n => $c):

                /* $deneme = $this->form_duzenle_model->cevapDuzenle($n,$c); */
                endforeach;
            }
        }


        redirect('form_duzenle/', 'refresh');
    }else {
        $this->bc->addCrumb('Düzenle');
        $veri = $this->form_duzenle_model->form_duzenleGetir($no)->row();
        $veri2 = $this->form_duzenle_model->cevapListe($no)->result();
        $data = array(
            'baslik' => $veri->baslik,
            'veri' => $veri,
            'cevap' => $veri2
        );
        $this->bc->addCrumb($veri->baslik, 'form_duzenle/duzenle/' . $veri->no);
        $this->layout->view('form_duzenle/form_duzenle_duzenle', $data);
    }
}

希望这有帮助!

答案 2 :(得分:-1)

我解决了这个问题.Duzenle代码更改底部代码。

function duzenle($no)
{

    if($_POST)
    {
            $arr1['baslik'] = $this->input->post('soru');
        $this->form_duzenle_model->duzenle($no,$arr1); 
        $cevaplar = $this->form_duzenle_model->cevapListe($no)->result_array();

    if($_FILES){

            $dizin=     "../upload/form_cevap/";
            foreach($cevaplar AS $cevap){
            $i = $cevap['no'];
            $isim= md5(uniqid(rand()));
            if(!empty($_FILES['cevap']['name'][$i])){
            move_uploaded_file($_FILES['cevap']['tmp_name'][$i],"./$dizin/$isim{$_FILES['cevap']['name'][$i]}");
            $arr['resim']=       $dizin.$isim.$_FILES['cevap']['name'][$i]; 
            }   
            $approve[] = $arr['resim'];   
            $it = $approve;
            $this->form_duzenle_model->cevapDuzenle($i,$arr['resim']); 
            }


    }

        redirect('form_duzenle/', 'refresh'); 
    }else{
        $this->bc->addCrumb('Düzenle');
        $veri   = $this->form_duzenle_model->form_duzenleGetir($no)->row();
        $veri2  = $this->form_duzenle_model->cevapListe($no)->result();
        $data   = array(
            'baslik'=>$veri->baslik,
            'veri'  =>$veri,
            'cevap' =>$veri2
            );
        $this->bc->addCrumb($veri->baslik,'form_duzenle/duzenle/'.$veri->no);
        $this->layout->view('form_duzenle/form_duzenle_duzenle',$data);
    }
}