表单验证以及db错误

时间:2014-12-10 09:19:23

标签: php mysql forms codeigniter validation

我在表单验证和db错误上收到错误。当我删除表单验证时,db查询工作完全正常。我不知道错误是什么以及如何解决它。

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: libraries/Form_validation.php

Line Number: 953

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ` = 'admin' LIMIT 1' at line 2

SELECT * WHERE ` = 'admin' LIMIT 1

Filename: C:\wamp\www\myblog.com\system\database\DB_driver.php

Line Number: 330

[Controller:myblog]

class Myblog extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('blogmodel');

        }


    public function login()
    {
        $this->load->view('header');
        $this->load->view('login');
        $this->load->view('footer');
    }
    public function login_check()
    {
        $user=$this->input->post("username");
        $pass=$this->input->post("password");
        $this->form_validation->set_rules('username', 'Username', 'trim|required|is_unique');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        if($this->form_validation->run()==true)
        {
            $this->blogmodel->checklogin($user,$pass);
            $this->load->view('header');
            $this->load->view('logsuccess');
            $this->load->view('footer');
        }
        else
        {
        $this->load->view('header');
        $this->load->view('login');
        $this->load->view('footer');
        }
    }

    public function reg()
    {       
        $this->form_validation->set_rules('username', 'Username', 'trim|required|is_unique');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        if($this->form_validation->run()==true)
        {   
            $user=$this->input->post("username");
            $pass=$this->input->post("password");
            $this->blogmodel->register($user,$pass);
            $this->load->view('header');
            $this->load->view('regsuccess');
            $this->load->view('footer');
        }

        else
            {
                redirect('myblog/login');           
   }
}





[Model: blogmodel]

class Blogmodel extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function checklogin($user,$pass)
    {
        $this->db->select('username, password');
        $this->db->from('user');
        $this->db->where('username', $user);
        $this->db->where('password', MD5($pass));
        $this->db->limit(1);

        $query = $this->db->get();
        if($query->num_rows() == 1) { 
            return $query->result(); 
        } else {
            redirect('myblog/login');
        }
    }

    function register($user,$pass)
    {
            $new_member=array(
            'username' => $user,
            'password' => md5($pass),
            'status' =>1
        );
        $insert = $this->db->insert('user', $new_member);
        return $insert;     
    }

[查看:登录]

   echo "<h2>Register</h2>";
   echo validation_errors();
   echo form_open('myblog/reg'); 
   echo form_label("Username: ");
   echo form_input("username");
   ?><br/>
   <?php
   echo form_label("Password:&nbsp;&nbsp;");
   echo form_password("password");
   ?><br/>
   <?php
   echo form_label("Confirm Password:&nbsp;&nbsp;");
   echo form_password("password2");
   ?><br/>
   <?php
   echo form_submit("","Register");
   echo form_close();


   echo "<h2>Login</h2>";
   echo validation_errors();
   echo form_open('myblog/login_check'); 
   echo form_label("Username: ");
   echo form_input("username");
   ?><br/>
   <?php
   echo form_label("Password:&nbsp;&nbsp;");
   echo form_password("password");
   ?><br/>
   <?php
   echo form_submit("","Login");
   echo form_close();

3 个答案:

答案 0 :(得分:2)

我的猜测是因为您还没有在表单验证中设置表格或行。

在您的控制器中更改此行;

    $this->form_validation->set_rules('username', 'Username', 'trim|required|is_unique');

到此;

    $this->form_validation->set_rules('username', 'Username', 'trim|required|is_unique[users.username]');

正如您所见,我已将[users.username]添加到is_inque规则中。没有它,CI不知道将它与之比较。

答案 1 :(得分:0)

SQL语句缺少要检查的表和列

SELECT * FROM table WHERE column = 'admin' LIMIT 1

答案 2 :(得分:0)

SQL语法不正确。 您的不完整的sql语句表示您要从包含您的用户名值的TABLE中获取COLUMN VALUE。 所以包括一个合适的列名和表名, 语法:

从table_name中选择*,其中column_name =&#39; admin&#39;限制1;