[英]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.