将数据从一个函数传递到另一个函数后,数据不会保存在数据库中

时间:2017-10-27 10:51:18

标签: php codeigniter

我在控制器中将数据变量从一个函数传递给另一个函数。它已成功通过,但当我尝试在第二个函数的if语句中使用它时,我得到未定义的变量错误。

这是我的职能编号1

public function otp()
    {
        //$this->input->post('email') ;
        //print_r($data['POST']);
        //exit();
        //$this->load->view('login/otp');       
        $success = "";
        $error_message = "";
        $conn = mysqli_connect("localhost","root","","fame");
        if(!empty($_POST["email"])) {

            $email = $_POST["email"];//I want to send this data
            //print_r($email);
            //exit();
            $this -> validate_otp($email);// I am sending the data here
    }

这是我的功能编号2

public function validate_otp($email) // Line 90
    {
    print_r($email);// Data is getting printed here
    exit(); //If I do this then the data is getting printed correctly upto this point. Once it gets into the below if statement it is showing the errors. 

    if ($this->agent->is_browser())
                {
                        $browser = $this->agent->browser().' '.$this->agent->version();
                        //echo $browser;
                        $ip = $this->input->ip_address();
                        //echo $ip;
                        $platform = $this->agent->platform();
                        $data = array(
                            'username' => $email,//Here this is getting undefined variable error (Line 120)
                            'browser' => $this->agent->browser().' '.$this->agent->version(),
                            'platform' => $this->agent->platform(),
                            'ip' => $this->input->ip_address()                          
                        );


                        $this->Login_info_model->add_record($data);
                        echo "You are successfully logged";
                        //exit();
                }
}

我不理解为什么在成功传递数据变量之后无法读取第二个函数的if语句中的变量。我们非常欢迎任何帮助。提前谢谢。

This is the error

This is the second error

我的Login_info_model

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_info_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }
    public function add_record($data){

        $this->db->insert('tab_login_info',$data);
    }
}

1 个答案:

答案 0 :(得分:0)

我可能错了,但是当你这样做时,它会打印出错误的变量:

public function validate_otp($email)
    {
    print_r($data);// Data is getting printed here

如果你这样做会怎么样?

public function validate_otp($email)
    {
    print_r($email);// Data is getting printed here

另一件事,这里缺少一些东西:

public function otp()
    {
        //$this->input->post('email') ;
        //print_r($data['POST']);
        //exit();
        //$this->load->view('login/otp');       
        $success = "";
        $error_message = "";
        $conn = mysqli_connect("localhost","root","","fame");
        if(!empty($_POST["email"])) {

            $email = $_POST["email"];//I want to send this data
            //print_r($email);
            //exit();
            $this -> validate_otp($email);// I am sending the data here
**} ->>> This was missing**
    }