我是CodeIgniter的初学者。我想在数据库中插入记录。这是我的视图文件。我将视图文件保存为register.php
,将我的模型文件保存为register_insert.php
,将控制器文件保存为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>
这是我的控制器文件..
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);
}
}
?>
}
?>
答案 0 :(得分:0)
我认为您需要在控制器中加载模型寄存器
public function __construct(){
parent::__construct();
$this->load->model('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');
}
}
在模型中,您可以在插入
后返回最后一个插入IDpublic function form_inssert($data){
$this->db->insert('register',$data);
return $this->db->insert_id();
}
答案 1 :(得分:0)
这是我的控制器文件
<?php
class Register extends CI_Controller{
public function index()
{
$this->load->model('register_insert');
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_insert extends CI_Model
{
function __construct()
{
parent::__construct();
}
function form_insert($data)
{
$this->db->insert('register', $data);
}
}
?>
我的错误是我的模型类名称的命名是“Register_insert”,我的旧类名是Register,这就是为什么我的模型没有正确加载..
答案 2 :(得分:0)
在您的控制器功能中,如下所示:
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);
redirect('register');
}
$this->load->view('register');
}
}
在您的模型文件中,如下所示:
function form_insert($data){
$this->db->insert('register', $data);
return true;
}
现在试试这个结果
答案 3 :(得分:0)
您可以尝试使用此视图代码:
<html>
<head>
<title>Register</title>
<style>
td {
border:none;
}
</style>
</head>
<table border="1" align="center">
<?php echo form_open('register/register_insert'); ?>
<tr>
<td>User name:<?php echo form_input('u_nm'); ?></td>
</tr>
<tr>
<tr>
<td>Password :<?php echo form_input('pwd'); ?></td>
</tr>
<tr>
<td>E-Mail: <?php echo form_input('eid'); ?></td>
</tr>
<tr>
<td>Contact No : <?php echo form_input('cno'); ?></td>
</tr>
<tr>
<td>Mobile No : <?php echo form_input('mbo'); ?></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Register"></td>
</tr>
<?php echo form_close(); ?>
</table>
</form>
</html>
</table>
使用表单助手更容易。您可以在autoload.php配置文件中自动加载,如下所示:
$autoload['helper'] = array('form');
这将是你的控制者:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class register extends CI_Controller {
public function index(){
$this->load->view('register');
}
public function register_insert()
{
//Here you can add validations for your form before trying to insert data to your Database
if(isset($_POST['submit']))
{
$this->load->model('register_insert');
$agregar=$this->register_insert->add_user();
}
$this->load->view('register');
}
}
?>
最后你的模型代码:
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class register_insert extends CI_Model {
public function __construct(){
parent::__construct();
}
public function add_user (){
$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->db->insert('register', $data);
}
}
对于此型号,您还需要更改autoload.php
您需要自动加载数据库库:
$autoload['libraries'] = array('database');