2016-07-22 49 views
1

这是我的代码,我的照片无法插入数据库。我不知道问题出在哪里。上传照片Failed Codeigniter(更新)

实际上我想用codeigniter进行在线考试。我想用图片上传问题。但是当我尝试上传图片时,代码无法正常工作。

但问题成功插入数据库。只有PICT上传失败

控制器:

function insert(){ 
 
\t \t $nama_asli = $_FILES['userfile']['name']; 
 
\t \t $config ['file_name'] = $nama_asli; 
 
\t \t $config ['upload_path'] = './images'; 
 
\t \t $config ['allowed_types'] = 'gif|jpg|png|jpeg'; 
 
\t \t $config ['max_size'] = '2500'; 
 
\t \t \t 
 
\t \t $this->load->library('upload', $config); 
 
\t \t \t 
 
\t \t $upload_data = $this->upload->data(); 
 
\t \t $file_name = $upload_data['file_name']; \t 
 
\t \t 
 
\t \t $id_soal = ''; 
 
\t \t $soal = $_POST['soal']; 
 
\t \t $a = $_POST['a']; 
 
\t \t $b = $_POST['b']; 
 
\t \t $c = $_POST['c']; 
 
\t \t $d = $_POST['d']; 
 
\t \t $kunci = $_POST['kunci']; 
 
\t \t $status = $_POST['status']; 
 

 
\t \t $data = array(
 
\t \t \t 'id_soal' => $id_soal, 
 
\t \t \t 'soal' => $soal, 
 
\t \t \t 'a' => $a, 
 
\t \t \t 'b' => $b, 
 
\t \t \t 'c' => $c, 
 
\t \t \t 'd' => $d, 
 
\t \t \t 'kunci' => $kunci, 
 
\t \t \t 'status' => $status, 
 
\t \t \t 'foto' => $file_name, 
 
\t \t \t); 
 

 
\t \t $hasil = $this->soal_model->Simpan('soal', $data); 
 

 
\t \t if($hasil>=1){ 
 
\t \t \t redirect('dashboard/index', $data); 
 
\t \t } 
 
\t }

型号:

class Soal_model extends Ci_Model { 
 

 
    public function Ambil($where= "") { 
 
    $data = $this->db->query('select * from soal '.$where); 
 
    return $data; 
 
    } 
 
    
 
    public function Simpan($tabel, $data){ 
 
    $res = $this->db->insert($tabel, $data); 
 
    return $res; 
 
    }

查看:

<form role="form" action="<?php echo base_url(); ?>dashboard/insert" method="POST" enctype="multipart/form-data"> 
 
<form class="form-horizontal" method="post" style = "margin : 10px;"> 
 
\t \t <div class = "row"> 
 
\t \t <div class = "col-sm-offset-3 col-sm-6"> 
 
\t \t <div class="form-group"> 
 
\t \t <label>Soal :</label> 
 
\t \t <textarea type="text" class="form-control" name="soal" id="soal" required></textarea> 
 
\t \t </div> 
 
\t \t 
 
\t \t <div class="form-group"> 
 
\t \t <label>Jawaban A :</label> 
 
\t \t <input type="text" class="form-control" 
 
\t \t placeholder="Type Here" name="a" id="a" required/> 
 
\t \t </div> 
 
\t \t 
 
\t \t <div class="form-group"> 
 
\t \t <label>Jawaban B :</label> 
 
\t \t <input type="text" class="form-control" 
 
\t \t placeholder="Type Here" name="b" id="b" required/> 
 
\t \t </div> 
 
\t \t 
 
\t \t <div class="form-group"> 
 
\t \t <label>Jawaban C :</label> 
 
\t \t <input type="text" class="form-control" 
 
\t \t placeholder="Type Here" name="c" id="c" required/> 
 
\t \t </div> 
 
\t \t 
 
\t \t <div class="form-group"> 
 
\t \t <label>Jawaban D :</label> 
 
\t \t <input type="text" class="form-control" 
 
\t \t placeholder="Type Here" name="d" id="d" required/> 
 
\t \t </div> 
 
\t \t 
 
\t \t <div class="form-group"> \t 
 
\t \t <label>Kunci :</label> 
 
\t \t <select name="kunci" id="kunci" class="form-control"> 
 
\t \t <option>Select</option> 
 
\t \t <option>A</option> 
 
\t \t <option>B</option> 
 
\t \t <option>C</option> 
 
\t \t <option>D</option> 
 
\t \t </select> 
 
\t \t </div> 
 
\t \t 
 
\t \t <div class="form-group"> \t 
 
\t \t <label>Status :</label> 
 
\t \t <select name="status" id="status" class="form-control"> 
 
\t \t <option value="">Select</option> 
 
\t \t <option value="tampil">Tampil</option> 
 
\t \t <option value="tidak">Tidak</option> 
 
\t \t </select> 
 
\t \t 
 
\t \t <div class="form-group"> \t 
 
\t \t <label>Photo :</label> 
 
\t \t <input type="file" name="foto" id="foto" size="20"/> 
 
\t \t </div> 
 
\t \t <br> 
 
\t \t 
 
\t \t <div class="form-group"> \t 
 
     <button type="submit" class="btn btn-default">Simpan</button> 
 
\t \t <button type="reset" class="btn btn-default">Hapus</button> 
 
\t \t </div> 
 
\t \t </div> 
 
\t \t </div> 
 
\t </form> 
 
\t 
 

数据库:

enter image description here

谁能帮助?

+0

返回结果是什么类型的错误? –

+0

你能显示你的html表单吗? –

+0

我已更新我的代码。你可以检查@Saravana Dev –

回答

0

请使用此代码

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class Dashboard extends CI_Controller{ 
    function __construct(){ 
     parent::__construct(); 
     $this->load->model('soal_model'); 
} 

function insert() 
{ 
    $config =array(
     'upload_path' => './images', 
     'allowed_types' => 'gif|jpg|png|jpeg', 
     'max_size' => '2500', 
    ); 
    $this->load->library('upload', $config); 

    $this->upload->do_upload('file_upload'); 

    $upload_data = $this->upload->data(); 

    $file_name = $upload_data['file_name']; 
    $data = array(
     'foto' => $file_name, 
    ); 

    $hasil = $this->soal_model->Simpan('soal', $data); 

    if($hasil>=1){ 
     redirect('dashboard/index', $data); 
    } 
} 
?>