1

I'm using CodeIgniter and I've got a function to insert an email address and a code into a database.

I now want the user to post that code and if it is correct, delete it, and if it is not correct, to give the user another try.

I've edited the original coding.

Email Controller - I've deleted reference to sessions and commented out the sending of the email because I'm only testing in localhost. The Email function works good. After posting the Email function I open the database and copy the code which must be correct.

Code Controller - I've deleted reference to sessions. And I now have an email text box in code view, which BTW automatically inserts the email address. I then paste the code into the code text box.

However, despite the code being correct the view('codeincorrect') is selected instead of view('username').

Can somebody tell me what is wrong?

Email Controller

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

Email Model

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

Code Controller

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

Code Model

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

This is the coding in the code view, which maybe causing the problem;

<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 Answers 2

1

Please try it.

In Controller

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

In Model

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);
}
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Md.Jewel Mia, I've tried that change you suggested but get this error; An uncaught Exception was encountered Type: Error Message: Call to undefined function form_open() Filename: C:\xampp\htdocs\application\views\codeincorrect.php Which refers to this in the View; <?php echo form_open('codeincorrect'); ?> The above works with other functions I've done.
However, the result from your coding goes to; $this->load->view('codeincorrect'); Whereas, the code was correct and it should go to; $this->load->view('username'); That View is the next step for the username and password.
It can't be working because it loads $this->load->view('codeincorrect');
Yeah because if don't response value in $result then load $this->load->view('codeincorrect'); if response value in $result then load $this->load->view('username');
What do you want me to check to find the response value?
|
1

So, your Controller calls the Model correctly but you are not checking the Model function and you are not sending the email field.

So this should be correct:

Controller:

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

}

Model:

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

}

Note: If what you are trying to do is a password login, that's not how you should do it. Please see this for example: http://www.iluv2code.com/login-with-codeigniter-php.html

4 Comments

Thanks Mr.Web, I've tried that change you suggested but get this error; An uncaught Exception was encountered Type: ParseError Message: syntax error, unexpected end of file, expecting function (T_FUNCTION) Its referring to; $this->db->delete('pass_word', $code); In the Model.
In response to your query, no I'm not trying to do a password login. I'm trying to first have the user post an email address which I succeed in getting the email address & code inserted into the database. I want an email sent with the code but that will have to wait until I have a server. However, I'm able to copy the code from the database then post it via the subject function. The code is simply held in the pass_word field temporally. The next step $this->load->view('username'); is for the permanent username and password.
OK, but I now get this error; Type: Error Message: Call to a member function num_rows() on array Filename: C:\xampp\htdocs\application\models\Code_model.php which refers to if ($code->num_rows() >= 1) {
I will tomorrow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.