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

tcbh2hod  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(168)
遇到PHP错误

严重度:通知
消息:未定义的索引:晚餐
文件名:cn.php
行号:18
回溯:
文件:C:\应用程序\模型\供应商_m. php
生产线:18
函数:错误处理程序
文件:C:\应用程序\控制器\供应商. php
生产线:42
功能:添加
文件:C:\文件夹
生产线:315
函数:需要一次(_O)

发生数据库错误

错误编号:1048
列'name'不能为空
INSERT INTO '供应商'('名称','电话','地址','描述')值(NULL,'81213176824',' xa',NULL)
文件名:/xampp/htdocs/系统/数据库/数据库驱动程序. php
行号:692
我的控制器:controllers/Supplier.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Supplier extends CI_Controller {

    
    function __construct()
    {
        parent::__construct();
        check_not_login();
        check_admin();
        $this->load->model('supplier_m');
    
    } 

    public function index()
    {
        $data['row'] = $this->supplier_m->get();
        $this->template->load('template', 'supplier/supplier_data', $data);
    }

    public function add()
     {
        $data['row'] = $this->supplier_m->get();
        $supplier = new stdClass();
        $supplier->supplier_id = null;
        $supplier->name = null;
        $supplier->phone = null;
        $supplier->address = null;
        $supplier->description = null;
        $data = array(
            'page' => 'add',
            'row' => $supplier
        );
        $this->template->load('template', 'supplier/supplier_form', $data);
    }

    public function process()
    {
        $post = $this->input->post(null, TRUE);
        if(isset($_POST['add'])) {
            $this->supplier_m->add($post);
        }

        if($this->db->affected_rows() > 0) {
            echo "<script>alert('Data berhasil disimpan');</script>";
        }
        echo "<script>window.location='".site_url('supplier')."';</script>";
    }

    public function del($id) {
        $this->supplier_m->del($id);
        if($this->db->affected_rows() > 0) {
            echo "<script>alert('Data berhasil dihapus');</script>";
        }
        echo "<script>window.location='".site_url('supplier')."';</script>";
    }
}

我的视图:views/supplier_form.php

<section class="content-header">
      <h1>Suppliers
        <small>Pemasok Barang</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i></a></li>
        <li><a href="#"></a></li>
        <li class="active">Suppliers</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">

    <div class="box">
        <div class="box-header">
            <h3 class="box-title">Add Supplier</h3>
            <div class="pull-right">
                <a href="<?=site_url('supplier')?>" class="btn btn-warning btn-flat">
                   <i class="fa fa-undo"></i> Back
                </a>
            </div>
         </div>
         <div class="box-body">
            <div class="row">
                <div class="col-md-4 col-md-offset-4">
                    <form action="<?=site_url('supplier/process')?>" method="post">
                        <div class="form-group">
                            <label>Supplier Name *</label>
                            <input type="text" name="supp" value="<?=$row->name?>" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label>Phone *</label>
                            <input type="number" name="phone" value="<?=$row->phone?>" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label>Address</label>
                            <textarea name="addr" class="form-control" required <?=$row->address?>></textarea>
                        </div>
                        <div class="form-group">
                            <label>Description</label>
                            <textarea name="desc" class="form-control" <?=$row->description?>></textarea>
                        </div>
                        <div class="form-group">
                            <button type="submit" name="<?=$page?>" class="btn btn-success btn-flat">
                               <i class="fa fa-paper-plane"></i> Save
                            </button>    
                            <button type="Reset" class="btn btn-flat">Reset</button>    
                        </div>
                
                    </form>

                </div>

            </div>
         </div>
         
            
    </div>

    </section>

我的模特:模特/供应商_m. php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Supplier_m extends CI_Model {

    public function get($id = null)
    {
        $this->db->from('supplier');
        if($id != null) {
            $this->db->where('supplier_id', $id);
        }
        $query = $this->db->get();
        return $query;
    } 

   public function add($post)
   {
    $params = [
        'name' => $post['supp'],
        'phone' => $post['phone'],
        'address' => $post['addr'],
        'description' => empty($post['desc']) ? null : $post['desc'],
    ];
    $this->db->insert('supplier', $params);

   }

    public function del($id)
    {
        $this->db->where('supplier_id', $id);
        $this->db->delete('supplier');
    }

}

请帮助我解决这个错误,我已经尝试,但仍然不能

sczxawaw

sczxawaw1#

对于第一个错误

您已经打开了Supplier_m模型并查找函数-〉公共函数add($post)
在函数开始时,您必须检查从控制器发送的数据,如

public function add($post){
  echo "<pre>";print_r($post);die;
}

您将获得过帐数据的实际结果

对于第二个错误

打开你的phpMyadmin(数据库)编辑表格,并选择复选框为空旁边的名称列,找到下面的图像可能会帮助你

相关问题