表单不会通过POST发送电子邮件地址两次

时间:2019-08-13 16:59:47

标签: php mysql codeigniter-3

我制作了优惠券生成器系统,为此您必须告知您的电子邮件地址。现在,我正在尝试制定一种小的算法,以检查电子邮件地址之前是否已经收到了优惠券。我有一个要求用户提供他的电子邮件的配方,它具有“ POST”方法,并且操作指向我的控制器。现在,如果我通知了一封全新的电子邮件(以前从未收到过优惠券的电子邮件),它将通常将该电子邮件存储在数据库的“电子邮件”表中并生成优惠券。

现在,如果我返回配方并通知相同的电子邮件地址,我什至无法将其与数据库中的电子邮件进行比较,以检查它是否已经存在,因为它不会进入第一个if(检查我的代码可以更好地理解)

如果电子邮件是新邮件,我不明白为什么会起作用,如果电子邮件已经存在,我不明白为什么。

我尝试在输入字段上使用print_r()来查看PHP如何读取它,并且仅(确切地说是这样)它返回不带引号的'email@email.com'。我尝试了没有第一个条件,实际上,我后来添加了它,因为我注意到MySQL给我一个与该电子邮件字段有关的错误,提示它不能为Null

也许这只是一个愚蠢的事情,我看不到它,因为我承受着一些压力并且无法正确思考

控制器:

<?php

class Cupom extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Popup_model');
        $this->load->model('Cupom_model');
        $this->load->model('Email_model');
    }

    function index()
    {
        if ($this->input->post('email_cupom')) {
            //CHECK IF IT IS ALREADY REGISTERED ON EMAILS' TABLE

            if ($this->Email_model->get_email_by_email($this->input->post('email_cupom'))) {
                //IF IT EXIST, SAVE IT IN THE VARIABLE BELOW (TURNING IT INTO AN ARRAY

                $email = $this->Email_model->get_email_by_email($this->input->post('email_cupom'));

                //CHECK IF THAT EMAIL HAS RECEIVED A COUPON BEFORE
                if ($email['recebeu_cupom'] == 0) {
                    $params_email = array(                         
                        'recebeu_cupom'  => 1,
                        );

                    $this->Email_model->update_email($email['id'], $params_email);

                    //THIS HERE JUST SAVES ON THE DATABASE IF THE USER INFORMED AN EMAIL
                    $ultimo_converted = $this->Popup_model->get_popup_by_popupid(1);

                    //GO IN DATABASE AND CHECK HOW MANY PEOPLE INFORMED THEIR EMAIL ON THAT POPUP

                    $novo_converted = intval($ultimo_converted['converted']) + 1;
                    $params = array(
                        'converted'  => $novo_converted ,
                        );
                    $this->Popup_model->update_popup('1', $params);

                    //GERA O CUPOM PROMETIDO POR ESSE POPUP
                    $params = array(
                        'codigo'  => rand(10000, 99999),
                        'descricao' => 'Cupom de primeira compra',
                        'unico' => '1',
                        'tipo'  => 'P',
                        'valor' => 20,
                        'status'=> '1'
                        );

                    //GRAVA O CUPOM NO BANCO DE DADOS
                    $cupom_id = $this->Cupom_model->add_cupom($params);

                    //BUSCA O CUPOM NO BANCO DE DADOS PELO ID
                    $cupom = $this->Cupom_model->get_cupom($cupom_id);
                    $data['cupom'] = $cupom['codigo'];
                    $data['_view'] = 'page/cupom';
                    $this->load->view('layouts/main', $data);
                } else {
                    redirect(current_url().'?cupom=1');
                }
            } else {
                $params_email = array(
                    'email'  => $this->input->post('email_cupom'),
                    'origem'  => 'Popup de Cupom',
                    'recebeu_cupom'  => 1,
                    );

                $this->Email_model->add_email($params_email);

                //PEGA POPUP PELO ID, CADA POPUP TEM ID DIFERENTE.
                $ultimo_converted = $this->Popup_model->get_popup_by_popupid(1);

                //BUSCA O NÚMERO DE CONVERSÃO DAQUELE POPUP E CONVERTE EM UM NÚMERO INTEIRO
                $novo_converted = intval($ultimo_converted['converted']) + 1;

                //GRAVA +1 CONVERTED PARA AQUELE POPUP
                $params = array(
                    'converted'  => $novo_converted ,
                    );
                $this->Popup_model->update_popup('1', $params);

                //GENERATES THE COUPON
                $params = array(
                    'codigo'  => rand(10000, 99999),
                    'descricao' => 'Cupom de primeira compra',
                    'unico' => '1',
                    'tipo' => 'P',
                    'valor' => 20,
                    'status'=> '1'
                    );

                //STORES THE COUPON ON DATABASE
                $cupom_id = $this->Cupom_model->add_cupom($params);

                //SENDS THE COUPON TO THE VIEW
                $cupom = $this->Cupom_model->get_cupom($cupom_id);
                $data['cupom'] = $cupom['codigo'];
                $data['_view'] = 'page/cupom';
                $this->load->view('layouts/main', $data);
            }
        } else {
            echo "<script>alert('não tem post')</script>";
        }
    }
 }

查看(仅表单):

 <form action="cupom" method="POST">

   <div class="row" style="height: 100px; ">
     <div class="col-md-6=8 col-md-offset-3">

    <label>Insira seu E-mail: </label>
<input type="email" name="email_cupom" placeholder="seuemail@email.com.br" style="..." required><br><br>

        </div>
    </div>

<button type="button" class="..." data-dismiss="modal">Não Quero Pagar Menos</button>
<button type="submit" class="..." style="...">RECEBER CUPOM</button>

<div class="...">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Não Quero Pagar Menos</button><br><br>

<button type="submit" class="..." style="...">RECEBER CUPOM</button>
    </div>
 </form>

模型(如果需要的话,我正在使用的功能):

class Email_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function get_email_by_email($email)
    {
        return $this->db->get_where('emails', array('email' => $email))->row_array();
    }
}

如果电子邮件已经收到优惠券,我会向页面发送获取值并打印一条消息,如果该获取值存在

0 个答案:

没有答案