发生数据库错误,错误号:1048'namabank'列不能为空

时间:2019-03-31 12:46:54

标签: php codeigniter

我看到一条错误消息:

发生数据库错误 错误编号:1048

“ namabank”列不能为空 插入 at Request.extractError... message: 'Transaction cancelled, please refer cancellation reasons for specific reasons [ValidationError, None]', code: 'TransactionCanceledException', time: 2019-03-31T00:03:05.401Z, requestId: 'URIARNJDVFHDVLQQNR1JMIJP5NVV4KQNSO5AEMVJF66Q9ASUAAJG', statusCode: 400, retryable: false, retryDelay: 30.057548210067033 mt_bankidbank)值(NULL,NULL)

文件名:C:/xampp5/htdocs/simastupen/system/database/DB_driver.php

行号:691

我在提交之前填写了表格。

idbank类型为int,是主键和自动递增,namabank类型为varchar

我的控制器:controller / bankdatel.php

namabank

我的视图:view / form / form_tambah_bank.php


public function tambahbank()
    {
        $this->load->view('template/header');
        $this->load->view('form/form_tambah_bank');
        $this->load->view('template/footer');

        $this->load->model('model_bankdatel');
        $arrdata = array(
            'idbank' => $this->input->post(''),
            'namabank' => $this->input->post('namabank')
        );

        $this->load->model('model_bankdatel');
        $this->model_bankdatel->insert($arrdata);
        $this->session->set_flashdata('info', "<script>alert('Data Bank Berhasil Disimpan');</script>");
        redirect('bankdatel');
    }

我的模型:model / model_bankdatel.php

            <form action="<?php echo site_url('bankdatel/tambahbank')?>" method="post">
              <div class="box-body">
                <div class="form-group">
                  <label>Nama Bank: </label>
                  <input name="namabank" type="text" class="form-control"  placeholder="">
                </div>

              <div class="box-footer">
                <button type="submit" class="btn btn-primary">Submit</button>
              </div>
            </form>

2 个答案:

答案 0 :(得分:0)

您应该检查是否有$_POST数据可用。如果是这样,请执行数据库插入,否则,当您每次调用加载形式的URL bankdatel/tambahbank时,插入查询将起作用。

public function tambahbank()
{

    if ($this->input->post()) { #Check post data available
        $this->load->model('model_bankdatel');
        $arrdata = array(
            'idbank' => $this->input->post(''),
            'namabank' => $this->input->post('namabank')
        );


        $this->model_bankdatel->insert($arrdata);
        $this->session->set_flashdata('info', "<script>alert('Data Bank Berhasil Disimpan');</script>");
        redirect('bankdatel');
    }
    $this->load->view('template/header');
    $this->load->view('form/form_tambah_bank');
    $this->load->view('template/footer');
}

您还应该实现form validation

答案 1 :(得分:-1)

该错误清楚地说明了问题,即namabank列不能具有空值。 转到phpmyadmin,然后取消选中namabank的null列以允许使用null。