表单验证和记录插入在Codeigniter中不起作用

时间:2018-10-12 10:22:57

标签: php mysql codeigniter

表单验证和记录插入在codeigniter中不起作用

模型

class Mymodel extends CI_Model
{

  function insert($data)
  {
    $this->db->insert("users", $data);
  }
}

控制器

defined('BASEPATH') OR exit('No direct script access allowed'); 
class Mycontroller extends CI_controller
{

    function index()
    {
        $this->load->view('myfolder/my_page');

    }

    function login()
    {
        $this->load->view('myfolder/login');
    }

    function signup()
    {
        $this->load->view('myfolder/signup');
    }

    function signupmethod()
    {

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

        $this->form_validation->set_rules("firstname","First Name",'requird|alpha');
        $this->form_validation->set_rules("lastname","Last Name",'requird|alpha');
        $this->form_validation->set_rules("email","Email",'requird|alpha');
        $this->form_validation->set_rules("password","Password",'requird|alpha');
        $this->form_validation->set_rules("mobile","Mobile",'requird|alpha');

        if ($this->form_validation->run()) 
        {
            $this->load->model("mymodel");

            $data = array(
                "firstname" => $this->input->post("firstname"),
                "lastname" => $this->input->post("lastname"),
                "email" => $this->input->post("email"),
                "password" => $this->input->post("password"),
                "mobile" => $this->input->post("mobile"),
                "curtime" => "NOW()"
            );

            if($this->input->post("Signup"))
            {
                $this->mymodel->insert($data);
                redirect(base_url() . "mycontroller/Inserted");
            }
        }
    }

    public function Inserted()
    {
        $this->index();
    }

}

   ?>

视图(html代码)

<?php include('header.php'); ?>

 <form method="post" action="<?php echo   base_url()?>mycontroller/signupmethod" enctype="multipart/form-data">
  <div class="container">

    <div class="row">
    <h3>Login</h3>
   </div>

     <div class="row">

        <div class="col-md-6 form-group">
        <label>First Name</label>
        <input type="text" name="firstname" value="" class="form-control">
        <span class="text-danger"><?php echo form_error("firstname"); ?></span>
        </div>

         <div class="col-md-6 form-group">
        <label>Last Name</label>
        <input type="text" name="lastname" value="" class="form-control">
        <span class="text-danger"><?php echo form_error("lastname");  ?></span>
        </div>

       </div>


<div class="row">

    <div class="col-md-6 form-group">
        <label>Email</label>
        <input type="text" name="email" value="" class="form-control">
        <span class="text-danger"><?php echo form_error("email"); ?></span>
    </div>

    <div class="col-md-6 form-group">
        <label>Password</label>
        <input type="password" name="password" value="" class="form-control">
        <span class="text-danger"><?php echo form_error("password"); ?></span>
    </div>

</div>


<div class="row">

    <div class="col-md-6 form-group">
        <label>Mobile</label>
        <input type="text" name="mobile" value="" class="form-control">
        <span class="text-danger"><?php echo form_error("mobile"); ?></span>
    </div>

    <div class="col-md-6 form-group" style="margin-top: 23px;">

        <input type="submit" name="submit" value="Signup" class="btn btn-info">
    </div>

</div>

我已经仔细阅读了每一行,代码运行了,但是没有显示错误,并且验证和记录插入均无效。而且没有错误显示。 请帮忙。

3 个答案:

答案 0 :(得分:2)

当您在控制器中获取表单元素时,请更改控制器此代码块,始终使用元素名称

if($this->input->post("submit"))
{
      $this->mymodel->insert($data);
      redirect(base_url() . "mycontroller/Inserted");
}

答案 1 :(得分:0)

必填项的正确拼写

$this->form_validation->set_rules("firstname","First Name",'required|alpha');

答案 2 :(得分:0)

您的set_rules语句不正确。使用required代替requird,否则set_rules将失败。

当您不确定为什么form_validation失败时,请尝试通过以下方式获取错误:

If($this->form_validation->run() == false)
{
    echo validation_error();
}