如果电子邮件ID已存在于数据库中,如何发送邮件?

时间:2017-08-22 07:18:41

标签: php codeigniter

代码:

<?php
    if($this->input->post('send'))
    {
        $email = $this->input->post('email');

        $this->db->select('password,firstname');
        $this->db->from('user');
        $where = "email = '$email'";
        $this->db->where($where);
        $query = $this->db->get();
        if ($query->num_rows() > 1)
        {
            $result = $query->result_array();
            foreach ($result as $row) 
            {
                $password = $row['password'];
                $firstname = $row['firstname'];
            }
            if($result)
            {
                $toEmail = $this->input->post('email');
                $fromEmail = "info@xyz.com";
                $this->load->library('email'); 
                $this->email->from($fromEmail, 'test'); 
                $this->email->to($toEmail);
                $this->email->subject('user Login Password'); 
                $body="Hi ".$firstname."Your Login Password is ".$password."";
                $this->email->message($body); 
                $this->email->send();
                if (!$this->email->send()) 
                { 
                    print_r($this->email->print_debugger(), true); 
                }
                else
                {
                    echo '<p style="color: red;font-weight: bold;">Error!.</p>';
                }
                echo '<p style="color: #41b212;font-weight: bold;">Kindly check your email to get your password.</p>';
            }
        }
        else
        {
            echo '<p style="color: red;font-weight: bold;">Your email id does not exist.</p>';
        }

    }
?>

<form method="post">
    <div class="form-group">
        <input type="text" name="email" id="email" placeholder="Enter Your Email" class="text-line"/>
    </div>
    <div class="form-group">
        <input type="submit" name="send" id="send" value="Send" class="btn btn-warning"/>
    </div>
</form>

在此代码中,我创建了一个重置​​密码表单。在这里,我有一个名为user的表。现在,当我点击发送按钮时,它始终显示您的电子邮件ID不存在。但是我正在创建如果用户电子邮件ID已经存在,它将向用户表中具有电子邮件ID的用户发送邮件。那么,我该怎么办呢?请帮帮我。

谢谢

1 个答案:

答案 0 :(得分:3)

您正在检查的情况有问题 变化

if ($query->num_rows() > 1)

if ($query->num_rows() > 0)