php - 无法在 CodeIgniter 中将记录插入数据库

标签 php mysql codeigniter

我是 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/

相关文章:

处理多个服务器的 PHP session

mysql - Node.js promise 和错误处理。这不能做得更干净吗?

php - 将下拉菜单中的值分配给 Controller 中的变量,该变量在 Codeigniter 的模型中使用

php - MYSQL不等于'<>'不起作用。怎么解决呢?

php - 重载 PHP 函数以首先执行操作

php - 如何让自适应图像为响应式 BootStrap 图像发送正确的文件大小?

php - 排序记录编号,然后按字母顺序排序

php - 如何使用 PHP 将 SQL 表导出为 excel 格式

mysql - 如果我检查日期 2019-01-12 是否在 12 月到 1 月的日期范围内,查询将给出空结果

php - Docker MySQL mysqli::real_connect():(HY000/2002):连接被拒绝