我是 CodeIgniter 的初学者。我想在数据库中插入记录。这是我的 View 文件。我将 View 文件保存为 register.php
,将模型文件保存为 register_insert.php
,将 Controller 文件保存为 register.php
,并将表名称保存在mysql 是“注册”。
查看
<?php $this->load->view('layot/header'); ?>
<html>
<head>
<title>Register</title>
<style>
td {
border:none;
}
</style>
</head>
<form method="POST" action="register">
<table border="1" align="center">
<tr>
<td>User Name : <input type="text" name="u_nm"></td>
</tr>
<tr>
<td>Password : <input type="text" name="pwd"></td>
</tr>
<tr>
<td>E-Mail: <input type="text" name="eid"></td>
</tr>
<tr>
<td>Contact No : <input type="text" name="cno"></td>
</tr>
<tr>
<td>Mobile No : <input type="text" name="mbo"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Register"></td>
</tr>
</table>
</form>
</html>
</table>
这是我的 Controller 文件..
public function index()
{
if(isset($_POST['submit']))
{
$data = array(
'u_nm' => $this->input->post('u_nm'),
'pwd' => $this->input->post('pwd'),
'eid' => $this->input->post('eid'),
'cno' => $this->input->post('cno'),
'mbo' => $this->input->post('mbo'),
);
$this->register_insert->form_insert($data);
}
$this->load->view('register');
}
}
?>
这是我的模型文件..
<?php
class Register extends CI_Model
{
function __construct() {
parent::__construct();
}
function form_insert($data){
$this->db->insert('register', $data);
}
}
?>
}
?>
最佳答案
我认为你需要在 Controller 中加载模型寄存器
public function __construct(){
parent::__construct();
$this->load->model('register');
}
在函数index中,需要调用类名register
public function index()
{
if(isset($_POST['submit']))
{
$data = array(
'u_nm' => $this->input->post('u_nm'),
'pwd' => $this->input->post('pwd'),
'eid' => $this->input->post('eid'),
'cno' => $this->input->post('cno'),
'mbo' => $this->input->post('mbo'),
);
$this->register->form_insert($data);
}
$this->load->view('register');
}
}
在模型中,您可以在插入后返回最后一个插入ID
public function form_inssert($data){
$this->db->insert('register',$data);
return $this->db->insert_id();
}
关于php - 无法在 CodeIgniter 中将记录插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29665169/