Php 什么';s通过表单发送数据的正确方式(codeigniter)

Php 什么';s通过表单发送数据的正确方式(codeigniter),php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,register.php-控制器 <?php class Register extends CI_Controller { public function index() { $this->load->helper('url'); $this->load->library('form_validation'); $this->load->view('header

register.php-控制器

<?php
class Register extends CI_Controller {

    public function index()
    {
                $this->load->helper('url');
      $this->load->library('form_validation');
                $this->load->view('header');
                $this->load->view('user/register');
                $this->load->view('home');
                $this->load->view('footer');

      $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
      $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]',
        array('required' => 'You must provide a %s.')
      );
      $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
      $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

      if ($this->form_validation->run() == FALSE) {
        $this->load->view('user/register');
      }
      else {
        header( 'Location: dashboard/lfg' ) ;
      }
    }
}

?>

register.php-查看

<div id="registration" class="modalDialog">
  <div>
    <a href="#close" title="Close" class="close">X</a>
    <h2>Create Account</h2>
    <?php echo validation_errors(); ?>
    <?php echo form_open('Register'); ?>
    <label>Username</label>
    <input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" required />
    <label>Email</label>
    <input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" required />
    <label>Password</label>
    <input type="password" name="password" value="<?php echo set_value('password'); ?>" size="50" required />
    <label>Confirm Password</label>
    <input type="password" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" required />
    <div>
      <input type="submit" value="Create Account" /></div>
  </form>
  <a class="modal-link" href="#login">
    <p>Already have an account?</p>
  </a>
</div>

创建帐户
用户名

必须在
控制器的末尾加载视图文件,如下所示:

<?php
class Register extends CI_Controller {

    public function index()
    {
        $this->load->helper('url');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]',
            array('required' => 'You must provide a %s.')
        );
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('header');
            $this->load->view('user/register');
            $this->load->view('home');
            $this->load->view('footer');
        }else {

            $data = $this->input->post();

            $allowed = array('username','email','password');

            $data = array_intersect_key($data,array_flip($allowed));

            //$data['password'] = do_hash($data['password']); ...

            $this->load->model('registration');
            $this->registration->form_insert($data);

            // set sessions and login data ... 

            redirect('dashboard/lfg');
        }
    }
}
您还可以在--construct()函数中只加载一次助手、库和模型

register.php-控制器

<?php
 class Register extends CI_Controller {
 function __construct()
{
    parent::__construct();

    $this->load->model('Registration');

}

        public function index()
        {
                    $this->load->helper('url');
          $this->load->library('form_validation');
                    $this->load->view('header');
                    $this->load->view('user/register');
                    $this->load->view('home');
                    $this->load->view('footer');

          $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
          $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]',
            array('required' => 'You must provide a %s.')
          );
          $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
          $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

          if ($this->form_validation->run() == FALSE) {
            $this->load->view('user/register');
          }
          else {
             $data= array("username"=>$this->input->post('username'),
            "password"=>$this->input->post('password'));
           $this->Registration->form_insert($data);
            header( 'Location: dashboard/lfg' ) ;
          }
        }
    }

    ?>


使用两个==进行表单验证,而不是三个===嘿,谢谢。我这样做会导致错误:遇到PHP错误严重性:注意消息:未定义属性:Register:$db Filename:core/Model.PHP行号:77 Backtrace:File:C:\xampp\htdocs\overwatch\application\models\registration.PHP行:8函数:u get File:C:\xampp\htdocs\overwatch\application\controllers\Register.PHP第28行函数:form_insert File:C:\xampp\htdocs\overwatch\index.php第315行函数:require_oncean和致命错误:在C:\xampp\htdocs\overwatch\application\models\registration.php第8行的null中调用成员函数insert(),遇到php错误严重性:错误消息:调用成员函数insert()在空文件名上:models/registration.php行号:8 Backtrace:@trunt您需要在
插入之前加载数据库:
$this->load->library('database')
或在
应用程序/config/autoload.php中
$autoload['libraries']=array('database').Great:)我很感激,这很有效。然而,目前还没有散列。我得弄清楚。
You can also load helper,library and model in --construct() function only one time

<?php
class Register extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->load->model('registration');
    }

    public function index()
    {


        $this->form_validation->set_rules('username', 'Username','trim|required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]',
            array('required' => 'You must provide a %s.')
            );
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('header');
            $this->load->view('user/register');
            $this->load->view('home');
            $this->load->view('footer');
        }else {

            $data = $this->input->post();

            $allowed = array('username','email','password');

            $data = array_intersect_key($data,array_flip($allowed));

            //$data['password'] = do_hash($data['password']); ...

            $this->registration->form_insert($data);

            // set sessions and login data ... 

            redirect('dashboard/lfg');
        }
    }
<?php
 class Register extends CI_Controller {
 function __construct()
{
    parent::__construct();

    $this->load->model('Registration');

}

        public function index()
        {
                    $this->load->helper('url');
          $this->load->library('form_validation');
                    $this->load->view('header');
                    $this->load->view('user/register');
                    $this->load->view('home');
                    $this->load->view('footer');

          $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
          $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]',
            array('required' => 'You must provide a %s.')
          );
          $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
          $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

          if ($this->form_validation->run() == FALSE) {
            $this->load->view('user/register');
          }
          else {
             $data= array("username"=>$this->input->post('username'),
            "password"=>$this->input->post('password'));
           $this->Registration->form_insert($data);
            header( 'Location: dashboard/lfg' ) ;
          }
        }
    }

    ?>