You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter3表单is_unique验证报错:表不存在,无法触发JS弹窗

问题:CodeIgniter3提交重复数据时未触发JS弹窗,反而出现数据库表不存在错误

在CodeIgniter3中尝试实现提交重复数据时弹出JavaScript提示,但提交重复数据时,未触发预期的JS弹窗,反而出现数据库错误提示:

A Database Error Occurred Error Number: 1146 in codeigniter, table doesn't exist

相关代码

控制器代码

function __construct()
{
    parent::__construct();
    //load model login_model
    $this->load->model('auth/auth_model');
    $this->auth_model->cek_login();
    $this->load->model("igd/M_Igd"); //load model data
    $this->load->library('PHPJasperXML');
    $this->load->library('form_validation');
}
public function input_aami_som(){
    $SUBJECTIVE = $this->input->post('SUBJECTIVE');
    $OBJECTIVE = $this->input->post('OBJECTIVE');
    $MASALAH = $this->input->post('MASALAH');
    $PLANNING = $this->input->post('PLANNING');
    $TINDAKAN_KONSUL = $this->input->post('TINDAKAN_KONSUL');
    $ASSESMENT = $this->input->post('ASSESMENT');
    $DESKRIPSI_ASSESMENT = $this->input->post('DESKRIPSI_ASSESMENT');
    $TANGGAL = $this->input->post('TANGGAL');
    $OLEH = $this->input->post('OLEH');
    $STATUS = $this->input->post('STATUS');
    $KUNJUNGAN = $this->input->post('KUNJUNGAN');

    
    $this->form_validation->set_rules('KUNJUNGAN', 'KUNJUNGAN', 'trim|required|is_unique[igd_assesment_awal_dokter.KUNJUNGAN]|xss_clean');
        

    if ($this->form_validation->run() == FALSE) {
        echo"<script>alert('Kenangan yang pernah ada')</script>";
    } else {
        $data = array(
        'SUBJECTIVE' => $SUBJECTIVE,
        'OBJECTIVE' => $OBJECTIVE,
        'MASALAH' => $MASALAH,
        'PLANNING' => $PLANNING,
        'TINDAKAN_KONSUL' => $TINDAKAN_KONSUL,
        'ASSESMENT' => $ASSESMENT,
        'DESKRIPSI_ASSESMENT' => $DESKRIPSI_ASSESMENT,
        'TANGGAL' => $TANGGAL,
        'OLEH' => $OLEH,
        'STATUS' => $STATUS,
        'KUNJUNGAN' => $KUNJUNGAN
        );
    $this->M_Igd->input_aami_som($data, 'igd_assesment_awal_dokter');
    $this->session->set_flashdata('message_aami_som','<div class="alert alert-success alert-dismissible mt-1 fade show" role="alert">
                                            Data berhasil dimasukkan
                                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                            </button>
                                            </div>');
    redirect($_SERVER['HTTP_REFERER']);
    }
}

模型代码

public function input_aami_som($data){
    $this->db->insert('igd_assesment_awal_dokter', $data);
}

视图代码

<form method="post" action="<?php echo base_url().'igd/igd/input_aami_som'?>">
    <input type="text" class="form-control" name="KUNJUNGAN" value="<?php echo $aami->NOMOR ?>" readonly>
    <input type="datetime-local" class="form-control" name="TANGGAL" value="<?php echo date("Y-m-d H:i:s") ?>">
    <input type="text" class="form-control" name="OLEH" value="<?php echo $session_user->nip?>" readonly> 
    <input type="text" class="form-control mb-1" name="STATUS" value="1" readonly>
    <?php echo $this->session->flashdata('message_aami_som');?>
    <div class="row">
        <div class="col-6">
            <label>Subjektif</label>
            <input type="text" class="form-control mb-1" name="SUBJECTIVE" required autocomplete="off">
            <label>Objektif</label>
            <input type="text" class="form-control mb-1" name="OBJECTIVE" required autocomplete="off">
            <label>Masalah</label>
            <input type="text" class="form-control mb-1" name="MASALAH" required autocomplete="off">
            <label>Planning</label>
            <input type="text" class="form-control mb-1" name="PLANNING" required autocomplete="off">
            <label>Tindakan/Konsultasi</label>
            <input type="text" class="form-control mb-1" name="TINDAKAN_KONSUL" required autocomplete="off">
            <div class="row mt-2">
                <div class="col-4">
                    <label class="form-check-label" for="">Assesment:</label>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="ASSESMENT" id="exampleRadios1" value="1">
                        <label class="form-check-label" for="exampleRadios1">
                            Diagnosa Awal 
                        </label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="ASSESMENT" id="exampleRadios2" value="2">
                        <label class="form-check-label" for="exampleRadios2">
                            Diagnosa Kerja
                        </label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="ASSESMENT" id="exampleRadios2" value="3">
                        <label class="form-check-label" for="exampleRadios2">
                            Diagnosa Banding
                        </label>
                    </div>
                </div>
                <div class="col-8">
                    <label class="form-check-label" for="">Deskripsi:</label>
                    <textarea class="form-control" id="exampleFormControlTextarea1" rows="3" name="DESKRIPSI_ASSESMENT" required autocomplete="off" placeholder="Masukkan deskripsi assesment yang dipilih di sini"></textarea>
                </div>
            </div>
            <button class="btn btn-sm btn-success mt-1 mb-1"> Masukkan</button>
        </div>
</form>

问题分析与解决方案

1. 解决数据库表不存在错误(Error 1146)

这是核心问题,is_unique验证规则需要查询指定表,表不存在会直接抛出数据库错误,根本无法执行后续的JS弹窗逻辑:

  • 检查数据库中是否存在igd_assesment_awal_dokter表,确认表名拼写完全一致(注意部分数据库区分表名大小写)
  • 核对application/config/database.php中的数据库配置,确保连接的是正确的数据库,用户名、密码无误

2. 修复表单验证失败后的页面逻辑

当前验证失败时直接输出JS弹窗,会导致页面空白,用户体验差,需修改控制器逻辑:

if ($this->form_validation->run() == FALSE) {
    // 自定义重复数据提示信息
    $this->form_validation->set_message('is_unique', '该就诊记录已存在');
    // 保存错误信息与表单输入值
    $this->session->set_flashdata('error_msg', validation_errors());
    $this->session->set_flashdata('old_input', $this->input->post());
    // 重定向回原页面
    redirect($_SERVER['HTTP_REFERER']);
}

然后在视图中添加弹窗提示与输入值恢复:

<?php if($this->session->flashdata('error_msg')): ?>
    <script>alert('<?php echo $this->session->flashdata('error_msg'); ?>');</script>
<?php endif; ?>

<!-- 示例:恢复SUBJECTIVE字段的输入值 -->
<input type="text" class="form-control mb-1" name="SUBJECTIVE" value="<?php echo $this->session->flashdata('old_input')['SUBJECTIVE'] ?? ''; ?>" required autocomplete="off">

3. 修正模型方法参数不匹配问题

控制器调用模型方法时传入了2个参数,但模型仅定义了1个参数,需二选一修改:

  • 修改模型方法接收表名参数:
public function input_aami_som($data, $table){
    $this->db->insert($table, $data);
}
  • 或控制器去掉冗余的表名参数:
$this->M_Igd->input_aami_som($data);

内容的提问来源于stack exchange,提问作者IMAM IHSANI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 17:01:13