Codeigniter表单验证回调函数

泰莎

我正在尝试创建一个表单验证回调函数,但是在解决这个问题时遇到了一些麻烦。

我想做的是创建一个联系表单,其中加入邮件列表选项。如果选中了加入邮件列表的选项,我希望将该人的姓名和电子邮件添加到邮件列表数据库中。这部分工作正常,但是我也希望该功能检查数据库以确保所添加的电子邮件地址是唯一的,而这就是我无法理解的地方。

控制器:

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('[email protected]');
        $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 '[email protected]' LIMIT 1' at line 3

SELECT * FROM `mail_list`, `email` WHERE mail_list, email '[email protected]' 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);

    }
Khairul伊斯兰教|

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

is_unique[table.field]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章