简体   繁体   English

A Database Error Occurred 错误编号:1048 列 'gambar' 不能是 null,上传文件 codeigniter

[英]A Database Error Occurred Error Number: 1048 Column 'gambar' cannot be null , upload file in codeigniter

the method upload file is work perfectly but i can't save the data, maybe there is something with variable "gambar", i just can't understand how this upload file works.上传文件的方法工作正常,但我无法保存数据,也许有一些变量“gambar”,我只是不明白这个上传文件是如何工作的。 so please help me to fix it所以请帮我解决它

在此处输入图像描述

controller: controller:

public function add(){
    if($this->input->post('submit')){
        if($this->Console_model->validation("save")){
        $this->Console_model->create();
        if($this->db->affected_rows() > 0){
            $this->session->set_flashdata('msg','<p  style="color:black">Console Succesfully Added!</p>');
        }else{
            $this->session->set_flashdata('msg','<p  style="color:red">Console Input Failed!</p>');
        }
        redirect('console');
        }
    }
    $data['specs']=$this->Spec_model->read();
    $data['view'] = "console/v_form";
    $this->load->view('index', $data);
}

model: model:

public function create(){
    $data=array(
        'nama_con' => $this->input->post('nama_con'),
        'garansi' => $this->input->post('garansi'),
        'deskripsi' => $this->input->post('deskripsi'),
        'harga' => $this->input->post('harga'),
        'gambar' => $this->input->post('gambar')
    );
    $this->gambar=$this->do_upload();
    $this->db->insert('tb_console',$data);
}

private function do_upload(){
    $config['upload_path']      = './uploads/barang/';
    $config['allowed_types']    = 'gif|jpg|png';
    $config['max_size']         = 4072;
    $config['max_width']        = 1024;
    $config['max_height']       = 768;

    $this->load->library('upload', $config);
    if($this->upload->do_upload('gambar')){
        return $this->upload->data('file_name');
    }
    return "default.jpg";
}

i have try to using the create method before i code the upload method and its works normal在我对上传方法进行编码之前,我曾尝试使用 create 方法,并且它的工作正常

view:看法:

<?php
$nama="";$gar="";$desk="";$har="";$gam="";
    if(isset($con)){
        $nama=$con->nama_con;
        $gar=$con->garansi;
        $desk=$con->deskripsi;
        $har=$con->harga;
        $gam=$con->gambar;
    }
?>
<div class="container">
    <div class="row">
        <form class="col-md-4 mx-auto" action="" method="post" enctype="multipart/form-data">
            <h2 class="mt-5 mb-5">Form Console</h2>
            <div style="color: red;"><?php echo validation_errors(); ?></div>
            <div class="form-group">
                <label for="Nama">Nama Console</label>
                <select name="nama_con" class="form-control" id="">
                    <option value="">Choose Console</option>
                    <?php foreach($specs as $spec){ ?>
                    <option value="<?=$spec->name_con?>" <?=set_select('name_con', $spec->name_con, $nama==$spec->name_con?TRUE:FALSE)?>><?=$spec->name_con?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="form-group">
                <label for="garansi">Garansi</label>
                <input type="number" class="form-control" name="garansi" value="<?=$gar?>">
            </div>
            <div class="form-group">
                <label for="Deskripsi">Deskripsi</label>
                <textarea name="deskripsi" class="form-control" id=""><?=$desk?></textarea>
            </div>
            <div class="form-group">
                <label for="harga">Harga</label>
                <input type="number" class="form-control" name="harga" value="<?=$har?>">
            </div>
            <div class="form-group">
                <label for="fullname">Gambar</label>
                <input type="file" name="gambar" class="form-control" required>
            </div>
            <div class="form-group"><input type="submit" name="submit" value="Save" class="btn btn-success">
            <a href="<?=site_url('console')?>"><input type="button" value="Batal" class="btn btn-danger"></a>
            </div>
        </form>
    </div>
</div>

Its cause your gambar value is null and your gambar field is not allowed null value, you may edit your field and give a check on null checkbox.它的原因是您的 gambar 值为 null 并且您的 gambar 字段不允许 null 值,您可以编辑您的字段并检查 null 复选框。

May you try to:你可以尝试:


public function create()
{
  $this->gambar=$this->do_upload();
  $data=array(
          'nama_con' => $this->input->post('nama_con'),
          'garansi' => $this->input->post('garansi'),
          'deskripsi' => $this->input->post('deskripsi'),
          'harga' => $this->input->post('harga'),
          'gambar' => $this->gambar
        );
   $this->db->insert('tb_console',$data);
}

your pict name isn't from post, its from do_upload return i think您的图片名称不是来自帖子,我认为它来自 do_upload 返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发生数据库错误错误号:1048 列“namabank”不能为空 - A Database Error Occurred Error Number: 1048 Column 'namabank' cannot be null 发生数据库错误错误号:1048 列 &#39;ket&#39; 不能为空 - A Database Error Occurred Error Number: 1048 Column 'ket' cannot be null 发生数据库错误错误号:1048 列“名称”不能为空 - A Database Error Occurred Error Number: 1048 Column 'name' cannot be null codeigniter中发生数据库错误,错误号:1048 - A Database Error Occurred In codeigniter Error Number: 1048 错误号:1048列&#39;btc&#39;不能为空 - Error Number: 1048 Column 'btc' cannot be null 成功提交表单,但控制台出现一个错误,如发生数据库错误错误编号:1048 - Codeigniter - Successfully submitted form but one error in console, like A Database Error Occurred Error Number: 1048 - Codeigniter Codeigniter:“错误1048列&#39;customer_id&#39;不能为空”为什么会出现此错误? - Codeigniter: “Error 1048 Column 'customer_id' cannot be null” Why am I getting this error? 发生数据库错误,列“ title”不能为空 - a database error occurred column 'title' cannot be null 发生数据库错误:Codeigniter中的错误号1054未知列 - Database Error Occurred: error number 1054 unknown column in codeigniter Codeigniter发生数据库错误,错误号:1064 - Codeigniter A Database Error Occurred Error Number: 1064
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM