Home > Back-end >  Otp is not post in database
Otp is not post in database

Time:02-21

after user submit mail id otp will be sent to that email id but my code isn't working it post the email id in database but not the otp. Please can any one help?

Controller Code auth.php

public function register()
{
    $alphabet = '1234567890';
    $otp = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache

    for ($i = 0; $i < 8; $i  ) {
        $n = rand(0, $alphaLength);
        $otp[] = $alphabet[$n];
    }
    $newotp = implode($otp);
    // $data['otp'] = $newotp;




    $to = $this->input->post('email');
    $subject = "OTP FOR LOGIN";
    $message = '
                           Dear,  User ,<br> <br>
                           
                            Thanks You For Requesting OTP.<br><br>
                            Your Username is - ' . $to . ' And <br><br>
                            Your Password Is - <b>' . $newotp . '</b> <br><br>
                           
                            Thank you!<br><br>  
                            This is an autogenerated email. Please do not reply to this email.
                            
                            <br><br>
                           
                          ';
    $mail = $this->Others->send_email($to, $subject, $message);
    $data = $this->user_model->insert('cred', ['email' => $to], ['otp' => ($newotp)]);

    redirect(base_url() . 'auth/login');
   
}

Model code user_model.php

public function insert($table, $data = array())
{

    $this->db->insert('cred', $data);
    
        $afftectedRows = $this->db->affected_rows();
        if ($afftectedRows == 1) {
            $id = $this->db->insert_id();
            return $id;
        } else {
            return FALSE;
        }
    
}

CodePudding user response:

In your controller you are actually passing three arguments to the $this->user_model->insert() instead of two. Second argument needs to be an array.

Try try updating following line with this:

$data = $this->user_model->insert('cred', ['email' => $to, 'otp' => $newotp]);
  • Related