简体   繁体   中英

Why is the result of my code blank after executing query, on this line $out = $query->result_array();

this is the image of database table. 在此处输入图片说明

I'm new at codeigniter PHP

public function ForgotPassword($email)
{ 

    $this->db->select('email');
    $this->db->from('member');
    $this->db->where('email', $email);
    $query=$this->db->get();
    $out = $query->result_array();

    if (count($out) > 0) {
        $email = $out[0]['email'];
        return array('status'=>1, 'email'=>$email); 
    } else {
        return array('status'=>0, 'msg'=>'email not found');
    }
}

This is my model function., this line giving me blank result $out = $query->result_array(); I don't know why, it should give me email address so that i can proceed and send email. I want to send password to user using forgot password function.

public function ForgotPassword()
{
    $email = $this->input->post('email');
    $findemail = $this->MY_model->ForgotPassword($email);

    if ($findemail['status'] == 1) {
        $this->MY_model->sendpassword($findemail);
    } else {
        echo " $email not found, enter correct email id";
    }
}

And this is my controller function. Here I'm getting blank value of $findemail . I don't know why.

public function sendpassword($data)
{
    $email = $data['email'];

    $query1=$this->db->query("SELECT * from member where email = '".$email."' ");
    $row=$query1->result_array();
    if ($query1->num_rows() > 0) {
        $passwordplain = "";
        $passwordplain  = rand(10000000,99999999);
        $newpass['password'] = md5($passwordplain);
        $this->db->where('email', $email);
        $this->db->update('member', $newpass);
        $mail_message='Dear '.$row[0]['username'].','. "\r\n";
        $mail_message.=' Your <b>Password</b> is 
                       <b>'.$passwordplain.'</b>'."\r\n";

        require 'C:\xampp\htdocs\phpmailer\class.phpmailer.php';
        require 'class.phpmailer.php';

        $mail = new PHPMailer;
        $mail->IsSendmail();
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = "smtp.gmail.com";
        $subject = 'Testing Email';
        $mail->AddAddress($email);
        $mail->IsMail();
        $mail->From = 'vishal@gmail.com';
        $mail->FromName = 'admin';
        $mail->IsHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $mail_message;
        $mail->Send();
        if (!$mail->send()) {
            echo "Failed to send password";
        } else {
            echo "Password sent ! check your mail";
        }
    } else {
        echo "Email not found!!!!";
    }
}

And also upper model function , it's not sending email. I'm using phpmailer to sending email. Please help.

If your model name is 'MY_model' then write construct function in your controller file and load the model into it as shown below.

function __construct() {
        parent::__construct();
        $this->load->model('my_model');
    }

And modify your ForgotPassword function of controller as below.

public function ForgotPassword()
{
$email = $this->input->post('email');
$findemail = $this->my_model->ForgotPassword($email);

if ($findemail['status'] == 1) {
    $this->my_model->sendpassword($findemail);
} else {
    echo " $email not found, enter correct email id";

}
}

I hope this helps.

I have made a function that checks if email exists in db. It is named CheckUser(). Then in ForgotPassword() you call CheckUser() ($email must be given as argument).

<?php
    public function CheckUser($email)
    {
        $response = $bdd->prepare('SELECT email FROM member WHERE email = :email');
        $response->execute([
          "email" => $$email
        ]);
        $data = $response->fetchAll();

        return $data[0];
    }

    public function ForgotPassword($email)
    {
        // here you call our new function

** UDPATE BELOW CALLING OF CHECKUSER FUNCTION **

        $out = $this->CheckUser($email);

        // sizeof will return the lenght of the returned data from our CheckUser() function
        if (sizeof($out) > 0){
            $email = $out[0]['email'];
            return array('status'=>1, 'email'=>$email); 
        }
        else {
            return array('status'=>0, 'msg'=>'email not found');
        }

    }

Let me know if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM