我在这上面浪费了好几个小时,我确信这很简单!
我对 codeigniter 还很陌生。
基本上,我想使用从表单收到的值执行简单的插入到我的数据库中,这是我的观点:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Page Title</title>
</head>
<body>
<div id="formage">
<?php echo validation_errors(); ?>
<?php echo form_open('../add_client'); ?>
<p>
<?php echo form_label('First Name', 'first_name');?>
<?php echo form_input('first_name', '', 'id="first_name"' );?>
</p>
<p>
<?php echo form_label('Last Name', 'second_name');?>
<?php echo form_input('second_name', '', 'id="second_name"' );?>
</p>
<p>
<?php echo form_label('Email Address', 'email_address');?>
<?php echo form_input('email_address', '', 'id="email_address"' );?>
</p>
<p>
<?php echo form_label('Password', 'password');?>
<?php echo form_password('password', '', 'id="password"' );?>
</p>
<p>
<?php echo form_label('Confirm Password', 'passwordconf');?>
<?php echo form_password('passwordconf', '', 'id="passwordconf"' );?>
</p>
<p>
<?php echo form_submit('submit', 'Add New Client');?>
</p>
<?php echo form_close(); ?>
</div>
这调用我的 Controller :
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Add_client extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'First Name', 'required');
$this->form_validation->set_rules('second_name', 'Second Name', 'required');
$this->form_validation->set_rules('email_address', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length(4)');
$this->form_validation->set_rules('passwordconf', 'Password', 'required|matches[password]');
if($this->form_validation->run() !== false){
$this->load->model('add_client_model');
$this->add_client_model->addUser(
$this->input->post('first_name'),
$this->input->post('second_name'),
$this->input->post('email_address'),
$this->input->post('password'));
}
$this->load->view('add_client_view');
}
}
这又从我的模型中调用 addUser 函数:
<?php
class Add_client_model extends CI_Model{
function __construct(){
}
public function addUser($fname, $sname, $email, $password){
$this->db->set('first_name', $fname);
$this->db->set('second_name', $sname);
$this->db->set('email_address', $email);
$this->db->set('pasword', sha1($name));
$this->db->insert('users');
}
}
我遇到的问题是,每当我提交表单并调用该函数时,它似乎都会像这样插入变量名称(参见第二行):
https://dl.dropbox.com/u/76853467/Picture%204.png
我似乎无法让它接受我插入函数参数中的发布数据!
有什么建议吗?非常感谢!
最佳答案
试试这个:
$data = array(
'first_name' => $fname,
'second_name' => $sname,
'email_address' => $email,
'password' => sha1($password)
);
$this->db->insert('users', $data);
关于php - codeigniter INSERT 仅插入变量名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11340467/