【问题标题】:Codeigniter Form Validation Callback FunctionCodeigniter 表单验证回调函数
【发布时间】:2015-07-04 02:05:20
【问题描述】:

我正在尝试创建一个表单验证回调函数,但我很难理解它。

我要做的是创建一个联系表单,其中包含加入邮件列表选项。如果选中加入邮件列表的选项,我希望将人的姓名和电子邮件添加到邮件列表数据库中。这部分工作完美,但是我还希望该功能检查数据库以确保添加的电子邮件地址是唯一的,这是我无法理解的一点。

控制器:

public function contact()
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    $this->form_validation->set_rules('name', 'your name', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));
    $this->form_validation->set_rules('email', 'your email address', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));

    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('templates/headder');
        $this->load->view('contact');
        $this->load->view('templates/footer');
    }
    else
    {
        $this->load->library('email');

        $name = $this->input->post('name');
        $email = $this->input->post('email');
        $phone = $this->input->post('phone');
        $message = $this->input->post('message');
        $list = $this->input->post('mailing_list');

        $email_message = "Name: $name<br>Email: $email<br>Phone: $phone<br>Message:<br>$message";

        $this->email->initialize();
        $this->email->from($email, $name);
        $this->email->to('myaddress@mydomain.co.uk');
        $this->email->subject('New Query');
        $this->email->message($email_message);
        $this->email->send();

        if($this->email->send()){
        $this->load->view('send_error');
        }
        else
        {
            if($list == 'no')
            {
            $this->load->view('sent');
            }
            else
            {
                $this->form_validation->set_rules('email', 'Email', 'is_unique[mail_list, email]');


                if($this->form_validation->run() == FALSE)
                {

                $this->load->model('mailing_listm');
                $this->mailing_listm->add_name();
                $this->load->view('sent'); 
                }
                else
                {
                $this->load->view('contact');
                }

            }
        }
    }
}

错误信息:

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 ' email 'myaddress@mydomain.co.uk' LIMIT 1' at line 3

SELECT * FROM `mail_list`, `email` WHERE mail_list, email 'myaddress@mydomain.co.uk' LIMIT 1

Filename: libraries/Form_validation.php

Line Number: 1134

希望有人能告诉我这次我做了什么愚蠢的事情。

另外,这个函数有点像怪物,这是我试过写的最复杂的东西。有什么办法可以将它拆分出来,让它由几个较小的函数而不是一个庞大的函数组成?

谢谢,

编辑 我已经根据下面关于使用 is_unique 的评论更新了我的代码,但是现在我收到一条错误消息。

编辑 型号:

Public function add_name()
    {
        $this->name = $this->input->post('name');
        $this->email = $this->input->post('email');

        $this->db->insert('mail_list', $this);

    }

【问题讨论】:

    标签: php codeigniter validation


    【解决方案1】:

    为了检查唯一字段,codeigniter 中有一个验证规则。

    is_unique[table.field]
    

    【讨论】:

    • 感谢您的回复。我已经更新了我的代码(请参阅我的原始帖子),但现在我在提交重复条目时收到错误消息。知道为什么吗?
    • 因为您在表名后使用了逗号(,) is_unique[mail_list, email]。您应该使用 dot(.)is_unique[mail_list.email] 表和列名之间没有任何空格。
    • 天啊!好的,我已经更正了,但现在我还有另一个错误“发生数据库错误,错误号:1062,重复条目 'Tessa' 用于键 'name_2',插入到 mail_list (name, email)值('Tessa'、'myaddress@mydomain.co.uk'),文件名:C:/xampp/htdocs/mywebsite/CI/application/models/mailing_listm.php,行号:16。"
    • 为什么要两次验证您的电子邮件!为什么不在控制器的顶部!
    • 因为我想在发送联系电子邮件之前验证是否提供了电子邮件地址,但我只想验证它是否是唯一的,如果此人想成为邮件列表的一部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 2017-12-02
    相关资源
    最近更新 更多