当我尝试打开时,无法在Codeigniter中查看表单页面。
form.php的 这是我在Codeigniter中的视图文件。我是Codeigniter的新手,我无法解决这个问题。
<!DOCTYPE html>
<html>
<head>
<link href="<?php echo base_url().'assets/bootstrap.min.css'; ?>" rel="stylesheet" type='text/css'>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.error{color:red}
</style>
</head>
<body>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-3">
<h1>Student Registraion</h1>
<?php
echo form_open();
echo form_label('First Name');
$data=[
'name'=>'fname',
'class'=>'form-control',
'id'=>'fname',
'value'=>isset($dta['fname'])?$dta['fname']:''
];
echo form_input($data);
echo form_error('fname');
echo form_label('Last Name');
$data=[
'name'=>'lname',
'class'=>'form-control',
'id'=>'lname',
'value'=>isset($dta['lname'])?$dta['lname']:''
];
echo form_input($data);
echo form_error('email');
echo form_label('Email');
$data=[
'name'=>'email',
'class'=>'form-control',
'id'=>'email',
'value'=>isset($dta['email'])?$dta['email']:''
];
echo form_input($data);
echo form_label('Mobile Number');
$data=[
'name'=>'mobile',
'class'=>'form-control',
'id'=>'mobile',
'value'=>isset($dta['mobile'])?$dta['mobile']:''
];
echo form_input($data);
echo form_error('mobile');
echo form_label('Message');
$data=[
'name'=>'message',
'class'=>'form-control',
'id'=>'message',
'value'=>isset($dta['message'])?$dta['message']:''
];
echo form_textarea($data).br(1);
echo form_submit('submit','submit','class="btn btn-success"');
echo form_close();
?>
</div>
<div class="col-md-3"></div>
</div>
</body>
</html>
home.php
当我尝试打开时,无法在Codeigniter中查看表单页面。
<?php
class home extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('select');
$this->load->helper(array('form','url','html'));
$this->load->library(array('form_validation','session'));
}
function records()
{
$ytl['dta']=$this->users->datas();
//$this->load->view('info',$ytl);
//echo "<pre>controller";print_r($ytl);
}
function primezone()
{
$ytl['dta']=$this->users->datas();
echo "<pre>controller";print_r($ytl);
}
function form($id)
{
//die($id);
if($this->input->post())
{
//echo"<pre>";print_r( $this->input->post());die;
$this->form_validation->set_rules('fname','First Name','required|min_length[5]');
$this->form_validation->set_rules('lname','Last Name','required|callback_check_picture[lname]');
//$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('mobile','Mobile Number','required|callback_valid_phone_number[mobile]');
$this->form_validation->set_error_delimiters('<h1 class="error">', '</h1>');
if($this->form_validation->run()==True)
{
$data=[
'fname'=>$this->input->post('fname'),
'lname'=>$this->input->post('lname'),
'email'=>$this->input->post('email'),
'mobile'=>$this->input->post('mobile'),
'message'=>$this->input->post('message'),
];
$ytl=$this->select->insertdata($data);
if($ytl)
{
$this->session->set_flashdata('message', 'Successfully Added.');
redirect('home');
}
}else
{
//$this->load->view('form');
}
}
$ytl['dta']=$this->select->getDataById($id);
//echo "<pre>";print_r($ytl);die;
$this->load->view('form',$ytl);
}
public function check_picture($a){
if(!is_numeric($a))
{
return true;
}else{
$this->form_validation->set_message('check_picture', 'Please enter only char value');
return False;
}
}
function valid_phone_number($value)
{
$value = strlen($value);
//echo $value;die;
if ($value == 10) {
return true;
}
else
{
$this->form_validation->set_message('valid_phone_number', 'Mobile number not in range'); //{10}
return false;
}
}
public function index()
{
//load the database
//$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h']=$this->select->select();
//return the data in view
$this->load->view('fetch_view', $data);
}
}
?>
select.php
这是我的模特。当我尝试打开时,无法在Codeigniter中查看表单页面。
<?php
class select extends CI_model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->database();
}
function datas()
{
$pzi=[
'name'=>'pratibha',
'email'=>'pratibha@gmail.com',
'mobile'=>998858585,
'address'=>'noida'
];
return $pzi;
}
function insertdata($data)
{
$this->load->database();
return $this->db->insert('student',
$data);
}
public function select()
{
//data is retrive from this query
$query = $this->db->get('student');
return $query;
}
function getDataById($id)
{
$this->db->select('*');
$this->db->where('id',$id);
$this->db->from('student');
$query = $this->db->get()->row_array();
return $query;
}
}
?>
答案 0 :(得分:0)
如果您要更新记录,则需要传入该记录的ID。然后,如果ID存在,您可以更新,否则您将按照当前的步骤进行插入。
所以在你的控制器中:
$data=[
'id'=>!empty($this->input->post('id')) ? $this->input->post('id') : null,
'fname'=>$this->input->post('fname'),
'lname'=>$this->input->post('lname'),
'email'=>$this->input->post('email'),
'mobile'=>$this->input->post('mobile'),
'message'=>$this->input->post('message'),
];
在你的模特中:
function insertdata($data)
{
if(!empty($data['id'])) {
$this->db->where('id', $data['id'])->update('student',$data);
return $data['id'];
} else {
unset($data['id']);
$this->db->insert('student', $data);
return $this->db->insert_id();
}
}
这假设您的表有一个名为id
的列。如果需要改变......
答案 1 :(得分:0)
您正在从控制器调用插入功能。调用更新函数
$this->db->where('id',$id);
$this->db->set($data);
$this->db->update('student');