在Codeigniter中将表单的值插入数据库 [英] Insert values of a form to database in codeigniter

查看:52
本文介绍了在Codeigniter中将表单的值插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我对Ignorace不满意,但codeigniter对我来说是新的。所以我不知道如何将值插入数据库。所以我的基本问题是我想了解如何将值从视图插入到控制器中,以便在数据库中建模。

Sorry for My Ignorace but codeigniter is new for me. SO I dont know how to insert values to database. So my basic question is I want to understand that how values are inserted from views to controller to model in a database.

这是我的控制器文件...

This is my controller file...

                <?php

                class Form extends CI_Controller {

                        public function index()
                        {
                                $this->load->helper(array('form', 'url'));

                                $this->load->library('form_validation');

                                $this->form_validation->set_rules('username', 'Username', 'required');
                                $this->form_validation->set_rules('password', 'Password', 'required',
                                        array('required' => 'You must provide a %s.')
                                );
                                $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
                                $this->form_validation->set_rules('email', 'Email', 'required');


                        }
                }?>

这是我的查看文件。

                    <?php

                    //print_r($data);

                    echo form_open();
                    ?>
                    <h5>Username</h5>
                    <input type="text" name="username" value="" size="50" />

                    <h5>Password</h5>
                    <input type="text" name="password" value="" size="50" />

                    <h5>Password Confirm</h5>
                    <input type="text" name="passconf" value="" size="50" />

                    <h5>Email Address</h5>
                    <input type="text" name="email" value="" size="50" />

                    <div><input type="submit" value="Submit" /></div>

                    </form>

这是模型文件。

                <?php

                class formsubmit extends CI_Model
                {




                }


                ?>

对不起,如果您也可以向我解释一下这个概念,那么..关于输入如何移至控制器然后是数据库。

sorry With Answer if you can explain me the concept also..on how the input is moving to controller and then database.

推荐答案

看,有几点...不要为您的控制器命名...这很糟糕想法...所有这些几乎都是直接来自文档的。我不是在重新发布您的视图代码...这是我在此处发布的非常基本的代码,并且有更好的处理方法,可能还有一些您想在继续之前在控制器中进行处理的事情,但是这至少应该让您入门。另外,不要命名模型和控制器,例如 form和 formsubmit……这不是一个好习惯……将它们命名为 User和 Users。

Look, a few points...don't name your controller Form...that's a bad idea...and all of this is pretty much straight from the documentation. I'm not reposting your viewcode... This is VERY basic code I'm posting here, and there are better ways of doing things, and probably some more things you are going to want to process in your controller before moving on, but this should at least get you started. Also, don't name your models and controllers things like "form" and "formsubmit" ...it's just not good practice...name them things like "User" and "Users."

此外,别忘了相应地调整您的路线文件。

Also, don't forget to adjust your routes file accordingly.

这是您的控制人

<?php

class User extends CI_Controller {
    public function index()
            {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required',
                                            array('required' => 'You must provide a %s.')
                                    );
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('index_view', $data);
        }
        else
        {
            $this->load->model('Users_model');
            $this->Users_model->insert_user();
            $this->load->view('your_success_view');
        }
    }
}
?>

这是您的模型:

<?php

class Users_model extends CI_Model
{
    public function insert_user()
    {    
        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),
            'email' => $this->input->post('email')
        );

        // users is the name of the db table you are inserting in
        return $this->db->insert('users', $data);
    }   
}
?>

这篇关于在Codeigniter中将表单的值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆