php 如何使用codeigniter将表单值插入到mysql数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10515046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Inserting Form values into mysql database using codeigniter
提问by Naveen
I am new to the Codeigniter.How to store the form values in mysql using codeigniter ,Can any one help me...Is there any links Can you provide.... Thanks In Advance.
我是 Codeigniter 的新手。如何使用 codeigniter 在 mysql 中存储表单值,任何人都可以帮助我...是否有任何链接可以提供.... 提前致谢。
回答by Anwar
Let me clearify you in an easy way...
让我以简单的方式为您澄清...
This will be your controller
这将是您的控制器
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
}
}
This will be your views. Befor creating views go to autoload.php and autoload url helper and database class. In your config.php set config['base_url'] = "path to your site"; form.php
这将是你的看法。在创建视图之前,请转到 autoload.php 并自动加载 url 帮助程序和数据库类。在你的 config.php 中设置 config['base_url'] = "path to your site"; 表单.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>
success.php
成功.php
<b>Your data has been inserted!!!</b>
In your model you need to pull those datas of form and insert into db this way
在您的模型中,您需要以这种方式提取表单数据并插入到数据库中
site_model.php
网站模型.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')");
}
}
In this case your database has three fields... modify query as per your requirements
在这种情况下,您的数据库具有三个字段...根据您的要求修改查询
回答by Jeremy Lawson
The CodeIgniter manual is pretty helpful.
CodeIgniter 手册非常有用。
Take a look at the Database Quickstart.
查看数据库快速入门。
回答by Adam
Go though the Database Class. It has all the functions used to manipulate the database. If you really want to learn codeigniter, take a few hours and go through the User Guide.

