我是Codeigniter的新手。如何使用Codeigniter将表单值存储在MySQL中?有人能帮我吗?是否有相关链接可提供?
谢谢您提前。
我是Codeigniter的新手。如何使用Codeigniter将表单值存储在MySQL中?有人能帮我吗?是否有相关链接可提供?
谢谢您提前。
让我以简单明了的方式向您解释...
这将是您的控制器
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
}
}
这将是您的视图。在创建视图之前,请转到autoload.php并自动加载url助手和数据库类。 在您的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>
success.php
<b>Your data has been inserted!!!</b>
在您的模型中,您需要以这种方式提取表单数据并将其插入到数据库中: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')");
}
}
在这种情况下,您的数据库有三个字段... 根据您的要求修改查询。