使用codeigniter插入数据库
嗨 我想在CI中使用表单插入数据,但我面临这个问题 这是我的模型代码使用codeigniter插入数据库,codeigniter,Codeigniter,嗨 我想在CI中使用表单插入数据,但我面临这个问题 这是我的模型代码 function add_form() { $this->load->database(); $id = $this->input->post('id'); $name = $this->input->post('name'); $age = $this->input->post('age'); $data = array(
function add_form() {
$this->load->database();
$id = $this->input->post('id');
$name = $this->input->post('name');
$age = $this->input->post('age');
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
);
$this->db->insert('user',$data);
}
这是我的控制器代码
function simpleform() {
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('welcomedb_model');
if( $this->input->post('submit') ) {
$this->welcomedb_model->add_form();
}
$this->load->view('welcomedb_view');
}
这是我的视图代码
<?php echo form_open('welcomedb/submit'); ?>
<? echo $name; ?>:
<? echo form_input('name'); ?>
</br>
<? echo $age; ?>:
<? echo form_input('age'); ?>
</br>
<?php echo form_submit('submit', 'Submit'); ?>
<?php echo form_close(); ?>
:
:
感谢您的帮助您的表单正在提交到
welcomedb/submit
,但您的控制器似乎位于welcomedb/simpleform
。。。也许你需要改变这一点
否则,似乎没有任何问题。可能:
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
);
从最后一个元素(年龄)中删除逗号,如下所示:
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age')
);
并且总是转储错误。最好的方法是执行这些代码。。。。。。
Best way is to do these code ......
First conifg the autoload and database.
into autoload:$autoload['libraries'] = array('database');
$autoload['helper'] = array('url','form','file');
Into controller
<?php
class CDemo extends CI_Controller
{
function index()
{
$this->load->view('VDemo');
}
function save()
{
$this->load->model('MDemo');
if($this->input->post('submit'))
{
$this->MDemo->process();
}
redirect('CDemo');
}
}
?>
Into Model
<?php
class MDemo extends CI_Model
{
function process()
{
$Id = $this->input->post('Id');
$Name = $this->input->post('Name');
$data = array(
'Id'=>$Id,
'Name'=>$Name
);
$this->db->insert('test',$data);
}
}
?>
Into View
<html>
<head>
<title>DEMO</title>
</head>
<body>
<h1>Form Biodata</h1>
<?php
echo form_open('CDemo/save', array('name' => 'VDemo'));
?>
<table>
<tr>
<td>Id :</td>
<td><input type="text" name="Id"></input></td>
</tr>
<tr>
<td>Name :</td>
<td><input type="text" name="Name"></input></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></input></td>
</tr>
</table>
<?php
echo form_close();
?>
<textarea rows="" cols="">Hello</textarea>
</body>
</html>
首先,关闭自动加载和数据库。
自动加载:$autoload['libraries']=array('database');
$autoload['helper']=数组('url','form','file');
进入控制器
成为模型
进入视野
演示
形态生物数据
身份证件:
姓名:
你好
查看代码:::请重新编辑您的问题并相应设置代码格式。你很接近,但在第一次尝试时就错过了。此外,您的视图代码应该在那里,而不是在注释中。感谢我编辑代码的帮助谢谢。。。问题到底是什么?问题是当我提交表单时,没有数据插入到数据库中,只有空值。最终我找到了解决方案。应该感谢您的帮助。这是PHP而不是JS!!通常认为在最后一个数组项中添加逗号是一种良好的做法。