通过表单(codeigniter)发送数据的正确方法是什么

时间:2017-01-18 01:06:36

标签: php codeigniter codeigniter-3

register.php - controller

julia> x'
1×2 RowVector{Int64,Array{Int64,1}}:
 1  1

julia> x'*y
2

register.php - view

<?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' ) ;
      }
    }
}

?>

registration.php - model

<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>

视图和控制器目前正常工作。表格&#34;提交&#34;如果输入的信息是正确的,即。密码匹配,电子邮件有效等,但我没有编写PHP来正确地将数据发送到数据库。我可以正确地使用stmt执行此操作,但我想使用框架来完成与数据库的所有操作,因为它内置了。

有没有人能够了解我需要做什么,或者可以给我一些示例代码,以便将其工作并将数据发送到数据库。

我已经检查了codeigniter文档,并了解了如何设置验证规则等,但似乎并没有考虑如何实际将数据发送到数据库。

如果有人有链接,那么它也会有所帮助。

3 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:background="@drawable/splashscreen">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Jaakkk"
            android:textColor="#ffffff" />
    </FrameLayout>
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

答案 1 :(得分:0)

您必须在controller的末尾加载视图文件,如下所示:

<?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');
        }
    }
}

答案 2 :(得分:0)

register.php - controller

<?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' ) ;
          }
        }
    }

    ?>