CodeIgniter表单中的数据检索

时间:2017-11-03 13:08:51

标签: codeigniter

我已成功在Code Igniter中创建了注册和登录系统。 注册后,所有用户数据都保存在数据库中。 我在登录表单中使用了电子邮件和密码,我想显示与该登录用户相关的所有详细信息。

我尝试了一些但没有得到确切的东西。我该怎么办?



((BouncyCastleProvider) Security.getProvider("BC")).addAlgorithm(ALG_ALIAS, NoneDigest.class.getName())






//Controller file

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller{
	function __construct()
	{
    	parent::__construct();
    	$this->load->helper(array('form', 'url'));
    	$this->load->library(array('session', 'form_validation', 'email', 'upload'));
    	$this->load->database();
    	$this->load->model('user_model');
        }
  public function index()
  {
    $this->load->view('registration');
  }

 public function registration()
  {  
    //validate input value with form validation class of codeigniter
    $this->form_validation->set_rules('fname', 'First Name', 'required|regex_match[/^[A-Za-z]{2,15}+$/]');
    $this->form_validation->set_rules('lname', 'Last Name', 'required|regex_match[/^[A-Za-z]{2,15}+$/]');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.email]');
    $this->form_validation->set_rules('phone', 'Phone Number', 'required|regex_match[/^[789]\d{9}$/]');
    $this->form_validation->set_rules('city', 'City', 'required|regex_match[/^[A-Za-z]{3,15}+$/]');
    $this->form_validation->set_rules('zip', 'Zip Code', 'required|regex_match[/^\d{6}$/]');
    $this->form_validation->set_rules('gender', 'Gender', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[15]');
    $this->form_validation->set_rules('confirmpswd', 'Password Confirmation', 'required|matches[password]');
    $this->form_validation->set_rules('image', 'Image', 'callback_do_upload','required');

        //$this->form_validation->set_message('is_unique', 'This %s is already exits');
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('registration', array('error' => ' ' ));
        }
        else
        {  

            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            $email = $_POST['email'];
            $phone = $_POST['phone'];
            $city = $_POST['city'];
            $zip = $_POST['zip'];
            $gender = $_POST['gender'];
            $password = $_POST['password'];
            $passhash = hash('md5', $password);
            $image = $_FILES['image']['name']; 
            //call method for uploading image
            $upload = $this->do_upload('image');
               

            //md5 hashing algorithm to decode and encode input password
            //$salt       = uniqid(rand(10,10000000),true);
      $saltid = md5($email);
      $data = array('fname' => $fname,
                    'lname' => $lname,
                    'email' => $email,
                    'phone' => $phone,
                    'city' => $city,
                    'zip' => $zip,
                    'gender' => $gender,
                    'password' => $passhash,
                    'image' => $image);

      if($this->user_model->insertuser($data))
      {
       $this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> Registration Successful</div>');
                    redirect(base_url().'user/login');
      }
      else
      {
        $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">
        								Something Wrong. Please try again ...</div>');
                    redirect(base_url());
      }
    }
}
 
 public function do_upload($data)
        {      
       
                $config['upload_path'] = './uploads/';
                
                $config['allowed_types'] = 'gif|jpg|png';
                $this->load->library('upload', $config);
                /*$config['max_size']             = 1000000;
                $config['max_width']            = 1024;
                $config['max_height']           = 768;*/

                 //base_url('uploads/')
                $this->upload->initialize($config); 

                if ( ! $this->upload->do_upload('image'))
                {   
                        $error = array('error' => $this->upload->display_errors());
                         
                        
                }
                else
                { 
                        $data = array('upload_data' => $this->upload->data());
                } 
                return $data;
        }
    

public function login()
{
    $this->load->view('login');
}
public function check_login()
{
    $email = $_POST['email'];
    $pass = hash('md5', $_POST['password']);
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[15]');
    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('login');
    }
    else
    {
        $res = $this->user_model->check_user($email , $pass);
        if(!empty($res))
        {
            echo "you are registered";
            echo "<br>";
            echo "<br>";
            echo "your details are as follows: ";
            echo "<br>";
            echo "<br>";
            echo "email:".$email;
            echo "<br>";
        }
        else
        {
            $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">email/password not found</div>');
        redirect(base_url().'user/login');
        }
    }
}
    function setSession($userId,$userName) {
        
        $userSession = array('userId'=>$userId,
                             'userName'=>$userName,
                             'loggedIn'=>TRUE );
        $this->session->set_userdata($userSession);
    }
   public function logout()
    {
        $this->session->sess_destroy();
        redirect(base_url().'user/login', 'refresh');
    }


}
&#13;
&#13;
&#13;

&#13;
&#13;
//View File

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Registration</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <style type="text/css">
    .form-box {
      max-width: 500px;
      position: relative;
      margin: 5% auto;
    }
  </style>
</head>

<body>
  <div class="wrapper">
    <div class="container">
      <div class="row">
        <div class="form-box">
          <div class="panel panel-primary">
            <div class="panel-heading text-center">
              <h3>Register</h3>
            </div>
            <?php 
                      echo $this->session->flashdata('msg');
                    ?>
            <div class="panel-body">
              <div class="row">
                <div class="col-sm-12">

                </div>
              </div>
              <form action="<?php echo base_url('user/registration');?>" method="post" enctype="multipart/form-data">

                <div class="row">
                  <div class="col-sm-6">
                    <div class="form-group">
                      <label class="control-label" for="fname">First Name</label>
                      <div>
                        <input type="text" class="form-control" id="fname" name="fname" placeholder="First Name" required="">
                        <span class="text-danger"><?php echo form_error('fname'); ?></span>
                      </div>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="form-group">
                      <label class="control-label" for="fname">Last Name</label>
                      <div>
                        <input type="text" class="form-control" id="lname" name="lname" placeholder="Last Name" required="">
                        <span class="text-danger"><?php echo form_error('lname'); ?></span>
                      </div>
                    </div>
                  </div>

                  <div class="col-sm-12">
                    <div class="form-group">
                      <label class="control-label" for="email">  Email</label>
                      <div>
                        <input type="email" class="form-control" id="email" name="email" placeholder="Email" required="">
                        <span class="text-danger"><?php echo form_error('email'); ?></span>
                      </div>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="form-group">
                      <label class="control-label" for="phone">Phone Number</label>
                      <div>
                        <input type="text" class="form-control" id="phone" name="phone" maxlength="10" placeholder="Phone Number" required="">
                        <span class="text-danger"><?php echo form_error('phone'); ?></span>
                      </div>
                    </div>
                  </div>

                  <div class="col-sm-6">
                    <div class="form-group">
                      <label class="control-label" for="city">City</label>
                      <div>
                        <input type="text" class="form-control" id="city" name="city" placeholder="City" required="">
                        <span class="text-danger"><?php echo form_error('city'); ?></span>
                      </div>
                    </div>
                  </div>

                  <div class="col-sm-6">
                    <div class="form-group">
                      <label class="control-label" for="zip">Zip Code</label>
                      <div>
                        <input type="text" class="form-control" id="zip" name="zip" placeholder="Zip Code" required="">
                        <span class="text-danger"><?php echo form_error('zip'); ?></span>
                      </div>
                    </div>
                  </div>

                  <div class="col-sm-6">
                    <div class="form-group">
                      <label class="control-label" for="gender">Gender</label>
                      <div>
                        <input type="radio" name="gender" value="male" id="gender" checked="true"> Male
                        <input type="radio" name="gender" value="female" id="gender"> Female
                        <input type="radio" name="gender" value="transgender" id="gender"> Transgender
                        <span class="text-danger"><?php echo form_error('gender'); ?></span>
                      </div>
                    </div>
                  </div>
                  <br>
                  <div class="col-sm-12">
                    <div class="form-group">
                      <label class="control-label" for="pswd">Password</label>
                      <div>
                        <input type="password" class="form-control" id="pswd" name="password" placeholder="Password" required="">
                        <span class="text-danger"><?php echo form_error('password'); ?></span>
                      </div>
                    </div>
                  </div>
                  <div class="col-sm-12">
                    <div class="form-group">
                      <label class="control-label" for="cn-pswd">Confirm Password</label>
                      <div>
                        <input type="password" class="form-control" id="cn-pswd" name="confirmpswd" placeholder="Confirm Password" required="">
                        <span class="text-danger"><?php echo form_error('confirmpswd'); ?></span>
                      </div>
                    </div>
                  </div>
                  <div class="form-group">
                    <div class="row">
                      <div class="col-sm-offset-5 col-sm-3  btn-submit">
                        <button type="submit" class="btn btn-success">Register</button>
                      </div>
                    </div>
                  </div>

                  <div class="col-sm-12">
                    <div class="form-group">
                      Select image to upload:<br>
                      <input type="file" name="image" id="image">
                      <br/><br/>
                    </div>
                  </div>
              </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

// While registering time your are using md5 encryption for password but while 
// retrieving time you are passing password without encryption formate
$data = $this->db->query($sql, array($email,$pass));
// Use this in your model
$data = $this->db->query($sql, array($email,hash('md5', $pass)));