[英]A Database Error Occurred Error Number: 1048 Column 'name' cannot be null
Severity: Notice严重性:通知
Message: Undefined index: supp消息:未定义索引:supp
Filename: models/Supplier_m.php文件名:models/Supplier_m.php
Line Number: 18行号:18
Backtrace:回溯:
File: C:\xampp\htdocs\sikan_v2\application\models\Supplier_m.php文件:C:\xampp\htdocs\sikan_v2\application\models\Supplier_m.php
Line: 18线路:18
Function: _error_handler函数:_error_handler
File: C:\xampp\htdocs\sikan_v2\application\controllers\Supplier.php文件:C:\xampp\htdocs\sikan_v2\application\controllers\Supplier.php
Line: 42线路:42
Function: add功能:添加
File: C:\xampp\htdocs\sikan_v2\index.php文件:C:\xampp\htdocs\sikan_v2\index.php
Line: 315线路:315
Function: require_once函数:require_once
Error Number: 1048错误号:1048
Column 'name' cannot be null列“名称”不能为空
INSERT INTO `supplier` (`name`, `phone`, `address`, `description`) VALUES (NULL, '81213176824', 'xa', NULL) INSERT INTO `supplier` (`name`, `phone`, `address`, `description`) VALUES (NULL, '81213176824', 'xa', NULL)
Filename: C:/xampp/htdocs/sikan_v2/system/database/DB_driver.php文件名:C:/xampp/htdocs/sikan_v2/system/database/DB_driver.php
Line Number: 692行号:692
My Controllers: controllers/Supplier.php我的控制器: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>";
}
}
My View: views/supplier_form.php我的观点: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>
My Models: models/Supplier_m.php我的模型:models/Supplier_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');
}
}
please help me solve this error, I have tried but still can't请帮我解决这个错误,我试过了还是不行
For first error对于第一个错误
you have open your Supplier_m model and find function -> public function add($post)你已经打开你的 Supplier_m 模型并找到 function -> public function add($post)
and at the beginning of the function you have to check the data which you have send from controller like在函数的开头,你必须检查你从控制器发送的数据,比如
public function add($post){
echo "<pre>";print_r($post);die;
}
you will get the actual result of post data您将获得发布数据的实际结果
For second error对于第二个错误
Open your phpMyadmin (databse) edit the table and select the checkbox as NULL beside the name column, find the below image may be it will help you打开您的 phpMyadmin(数据库)编辑表格并选择名称列旁边的复选框为 NULL,找到下图可能会对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.