如何检查数据库中的数据是否为真? - 不在CodeIgniter中使用Session

时间:2017-05-25 08:12:37

标签: codeigniter

我正在使用CodeIgniter,并且我已经有了将电子邮件地址和代码插入数据库的功能。

我现在希望用户发布该代码,如果正确,请将其删除,如果不正确,请再次尝试使用。

我已经编辑了原始编码。

电子邮件控制器 - 我已删除对会话的引用并注释了电子邮件的发送,因为我只在localhost中进行测试。电子邮件功能很好用。发布电子邮件功能后,我打开数据库并复制必须正确的代码。

代码控制器 - 我删除了对会话的引用。我现在在代码视图中有一个电子邮件文本框,BTW会自动插入电子邮件地址。然后我将代码粘贴到代码文本框中。

但是,尽管代码正确,但选择了视图(' codeincorrect')而不是视图('用户名')。

有人可以告诉我出了什么问题吗?

电子邮件控制器

class Email extends CI_Controller
{
public function index()
{
$this->load->model('Email_model', 'email_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email', 'required|min_length[10]|max_length[40]|valid_email|is_unique[tbl_members.email_address]', array(
'required' => 'You have not entered an %s address.', 'min_length' => 'Your %s address must be a minimum of 10 characters.',
'max_length' => 'Your %s address must be a maximum of 40 characters.', 'valid_email' => 'You must enter a valid %s address.',
'is_unique' => 'That %s address already exists in our Database.'));
if ($this->form_validation->run() == FALSE) // The email address does not exist.
{
$this->load->view('email');
}
else
{
$email = $this->input->post('email');
$random_string = chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . chr(rand(65,90));
$code = $random_string;
$this->email_model->insert_email($email, $code);
/*
$this->load->library('email');
$this->email->from('<?php echo WEBSITE_NAME; ?>', '<?php echo WEBSITE_NAME; ?>');
$this->email->to('$email');
$this->email->subject('Code.');
$this->email->message('Select & Copy this code, then return to the website. - ','$code');
$this->email->send();
*/
$this->load->view('code');
}
}
}

电子邮件模型

class Email_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
public function insert_email($email, $code)
{
$data = array('email_address' => $email,'pass_word' => $code);
$this->db->insert('tbl_members', $data);
return $this->db->insert_id();
}
}

代码控制器

class Code extends CI_Controller
{
public function index()
{
$this->load->model('Code_model', 'code_model');
$email = $this->input->post('email');
$code = $this->input->post('code');
$result = $this->code_model->find_code($email, $code);
if ($result)
{
$this->load->view('username');
}
else
{
$this->load->view('codeincorrect');
}
}
}

代码模型

class Code_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
public function find_code($email, $code)
{
$this->db->select('user_id');
$this->db->where('email_address', $email);
$this->db->where('pass_word', $code);
$code = $this->db->get('tbl_members');
if ($code->result())
{
return $this->db->delete('pass_word', $code);
}
}
}

这是代码视图中的编码,可能会导致问题;

<style type="text/css"> .email-address { position: fixed; width: 100%; text-align: center; top: 30%; } </style>
<div class="email-address">
<input type="text" name="email" value="<?php echo set_value('email'); ?>" style="width: 18%; height: 5mm"; />
<style type="text/css"> .code { position: fixed; width: 100%; text-align: center; top: 55%; } </style>
<div class="code">
<input type="email" class="form-control" name="code" style="width: 15mm; height: 5mm"; />
<a href="<?php echo BASE_URL . 'username'; ?>"></a></div>
<?php echo form_open('code'); ?>
<style type="text/css"> .submit { position: fixed; width: 100%; text-align: center; top: 65%; } </style>
<div class="submit">
<input type="submit" class="btn btn-primary" value="Submit" />
</form></div>

2 个答案:

答案 0 :(得分:1)

因此,您的Controller正确调用了Model,但您没有检查Model函数,并且您没有发送电子邮件字段。

所以这应该是正确的:

<强>控制器:

public function index()
{
    $this->load->model('Code_model', 'code_model');
    $code = $this->input->post('code');
    $email= $this->input->post('email');

    if ($this->code_model->find_code($code, $email)) {
        $this->load->view('username');
    } else {
        $this->load->view('codeincorrect');
    }

}

<强>型号:

public function find_code($code, $email)
{
    $this->db->select('user_id');
    $this->db->where('email_address', $email);
    $this->db->where('pass_word', $code);
    $code = $this->db->get('tbl_members')->result();
    if ($code->num_rows() >= 1) {
        $this->db->delete('pass_word', $code);
    }

}

注意: 如果你要做的是密码登录,那不是你应该怎么做。请参阅此示例:http://www.iluv2code.com/login-with-codeigniter-php.html

答案 1 :(得分:1)

请试试。

在控制器

public function index()
{

  $this->load->model('Code_model', 'code_model');

  $code = $this->input->post('code');
  // Need to email here.
   $email= $this->input->post('email');
   $result = $this->code_model->find_code($code,$email);

  if ($result)
  {
    $this->load->view('username');
  }
  else
  {
    $this->load->view('codeincorrect');
  }
}

在模型中

public function find_code($code,$email)
{
$this->db->select('user_id');
$this->db->where('email_address', $email);
$this->db->where('pass_word', $code);
$code = $this->db->get('tbl_members');
if ($code->result()) {
$this->db->delete('pass_word', $code);
}
}