如何使用codeigniter将表单值插入到mysql数据库中

Nav*_*een 4 php codeigniter

我是Codeigniter的新手.如何使用codeigniter将表单值存储在mysql中,任何人都可以帮助我...是否有任何链接可以提供....在此先感谢.

小智 14

让我以一种简单的方式澄清你......

这将是你的控制器

class Site extends CI_Controller
{
function index()
{
$this->load->view('form.php');// loading form view
}

function insert_to_db()
{
$this->load->model('site_model');
$this->site_model->insert_to_db();
$this->load->view('success');//loading success view
}
}
Run Code Online (Sandbox Code Playgroud)

这将是你的看法.创建视图请转到autoload.php和autoload url helper和数据库类.在你的config.php中设置config ['base_url'] ="你网站的路径"; form.php的

<form action="<?php echo base_url();?>index.php/site/insert_into_db" method="post">
Field 1 = <input type = 'text' name='f1'>
Field 2 = <input type = 'text' name='f2'>
Field 3 = <input type = 'text' name='f3'>
<input type='submit'>
</form>
Run Code Online (Sandbox Code Playgroud)

success.php

<b>Your data has been inserted!!!</b>
Run Code Online (Sandbox Code Playgroud)

在您的模型中,您需要拉取表单的数据并以这种方式插入db

site_model.php

class Site_model extends CI_Model
{
function insert_into_db()
{
$f1 = $_POST['f1'];
$f2 = $_POST['f2'];
$f3 = $_POST['f3'];
$this->db->query("INSERT INTO tbl_name VALUES('$f1','$f2','$f3')");
}
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您的数据库有三个字段...根据您的要求修改查询