John Davidson

php - Otp is not post in database

0 comments
Message:


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;
}

}


My Answer:
= 6; $i++) {_ $n = rand(0, $alphaLength);_ $otp[] = $alphabet[$n];_ }_ $otp = implode($otp);_ $data = array('email' => $this->input->post('email'), 'otp' => $otp);_ $this->auth_model->register($data);_ // send OTP to email code here_}_Model Code auth_model.php_public function register($data)_{_ $this->db->insert('users', $data);_}In the controller code, you are generating the OTP correctly and storing it in the $otp variable. However, you are overwriting the $otp variable with the implode function, which converts the array into a string. Instead, you should store the generated OTP in a different variable before imploding it.

Here is the corrected code:

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

for ($i = 0; $i < 6; $i++) {
$n = rand(0, $alphaLength);
$otpArray[] = $alphabet[$n];
}

$otp = implode($otpArray);

$data = array('email' => $this->input->post('email'), 'otp' => $otp);
$this->auth_model->register($data);
// send OTP to email code here
}


By storing the generated OTP in the $otpArray variable before imploding it, you can now correctly store the OTP in the database.

Rate this post

4 of 5 based on 5723 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us