问题:-即使使用form_validation,它也在表空白中插入数据我试过如果条件检查cat_title然后它工作所以基本上我想不使用if竞争就使用它。我认为问题出在我的控制器上。
这是我的控制器
class Cat extends CI_Controller {
//insert in table
public function addcat()
{
$this->form_validation->set_rules('cat_title', 'Category', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/addcategory');
}
else
{
$this->load->view('admin/addcategory');
}
$data['cat_title'] = $this->input->post('cat_title');
$this->cat_model->create_cat('category',$data);
}
// print data
public function showcat()
{
$data['result'] = $this->cat_model->get_cat();
$this->load->view('admin/showcategory',$data);
}
}
?>
这是我的模特
<?php
class Cat_model extends CI_Model {
// get table from database
public function get_cat(){
$query = $this->db->get('category');
return $query->result();
}
public function create_cat($table,$data){
//insert data in table
$query = $this->db->insert($table,$data);
return $query;
}
}
?>
这是我的HTML表单,我使用codeigniter创建表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<?php $this->load->view('includes/admincss');?>
</head>
<style>
</style>
<body>
<?php $this->load->view('includes/adminheader');?>
<section>
<div class="col-lg-12 mrg-top">
<div class="row">
<?php $this->load->view('includes/header_nav');?>
<div class="col-lg-10">
<h1>Add Category</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('cat/addcat'); ?>
<div class="form-group">
<label for="pwd">Type in below Input</label>
<input type="text" class="form-control" name="cat_title">
</div>
<a href=""><button type="submit" name="submit" class="btn btn-dark">Submit</button></a>
<?php echo form_close(); ?>
</div>
</div>
</div>
</section>
<?php $this->load->view('includes/adminfooter');?>
</body>
</html>
例如,MySQL列不是null设置,如果该列可以为null,则您的代码有效
ALTER TABLE table_name MODIFY mycolumn varchar(255) null;