I am working on a registration form
When i submit the form after the entering details the registration data get stored in the database but the the page shows the following error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: dbRet
Filename: models/registration_model.php
Line Number: 47
I am not getting the prolem the same code is working good at the localhost but what is the problem at server??
This is the model Code
class Registration_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function index()
{
$data['page_title'] = 'Registration';
$this->load->view('client/general/head',$data);
$this->load->view('client/general/header');
$this->load->view('registration');
$this->load->view('client/general/footer');
}
function busregister()
{
$data['page_title'] = 'Registration';
$this->load->view('business/registration1');
}
public function save_registration($un,$email,$pwd,$utype)
{
try
{
$data = array(
'username' => $un,
'password' => $pwd,
'email' => $email
);
//print_r($data);
//die();
$this->load->database();
$this->db->reconnect();
if($utype=='buss')
{
$dbRet=$this->db->insert('business', $data);
}
else
{
$dbRet-$this->db->insert('user', $data);
}
if (!$dbRet) {
$ret['ErrorMessage'] = $this->db->_error_message();
$ret['ErrorNumber'] = $this->db->_error_number();
log_message('error', "DB Error: (".$ret['ErrorNumber'].") ".$ret['ErrorMessage']);
$this->db->close();
return $ret['ErrorNumber'];
}
else{
$rid=$this->db->insert_id();
$this->db->close();
return $rid;
}
}
catch (Exception $e){
return -1;
}
}
}?>
This is the controller code
class Registration extends CI_Controller {
public function index()
{
$this->load->helper(array('registration','date','country'));
$this->load->helper('client');
$this->load->model('Registration_model');
if ($this->input->post("submit_registration",TRUE))
{
if($this->form_validation->run("stud_registration_rules"))
{
$this->load->library(array('encrypt'));
$stud_first_name = $this->input->post('stud_user_name');
$stud_email = $this->input->post('stud_email');
$stud_pass = $this->input->post('stud_password');
$retval=$this->Registration_model->save_registration($stud_first_name,$stud_email,$this->encrypt->encode($stud_pass),"user");
var_dump($this->encrypt->encode($stud_pass));
if($retval!==-1){ //SAVE SUCCESS
$this->session->set_flashdata('flashSuccess', 'Record saved successfully.');
redirect('/');
}
else{
$this->session->set_flashdata('flashError', 'Error Saving Record');
redirect('/registration');
}
}
else{
// FORM IS NOT VALIDATED
$this->session->set_flashdata('flashError', 'Validation fields error');
redirect('/registration');
}
}
$this->Registration_model->index();
}
}
This is the view code
<div class="content">
<div class="container_12">
<div class="grid_12">
<?php if($this->session->flashdata('flashSuccess')) : ?>
<div class="alert alert-success">
×
<p><?php print_r($this->session->flashdata('flashSuccess'));
?></p>
</div>
<?php endif; ?>
<?php if($this->session->flashdata('flashError')) : ?>
<div class="alert alert-danger">
×
<p><?php print_r($this->session->flashdata('flashError'));
?></p>
</div>
<?php endif; ?>
<?php echo registration_form();?>
</div>
</div>
In your Model code Registration_model , Else part assignment operator is wrong
change the following line
$dbRet-$this->db->insert('user', $data);
to
$dbRet = $this->db->insert('user', $data);
Related
I am new in CI and I have make login system but I am trying to show user profile data in username and email are show on user profile view page. I am to much try but I cant solve this issue. Here is my code.
Can one tell me about how to show user profile data in profile view page? I have too much trying but no solution is available for showing a user profile data in codeigniter.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class UserLogin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper(array('form', 'url'));
$this->load->model('login_model');
}
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->view('UserLoginPage');
}
public function userLoginProcess()
{
// $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
// echo "login reached";
$this->form_validation->set_rules('username', 'Username', 'required|alpha|trim');
$this->form_validation->set_rules('password', 'Password','required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");
$this->session->set_flashdata("<p class='text-danger'>","</p>");
if ($this->form_validation->run() ){
//username aur password araha hay
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('login_model');
//$loginObj session ko check variable haa..!
$loginObj = $this->login_model->login_valid($username,$password);
if($loginObj){
// print_r($loginObj->password);
$this->session->set_userdata('userSessionObj', $loginObj);
//print_r($loginObj);
$this->load->view('userDashboard');
}
else{
// echo "<script>alert('UserName And Passowrd Are Wrong....!!!! ');</script>";
// $this->session->set_flashdata('error', 'Invalid Username and Password');
$this->load->view('UserLoginPage');
// echo "<script language=\"javascript\">alert('Username And Password Are Worng');</script>";
$this->session->set_flashdata('error','<p class="text-danger"> you entered invalid username and password');
} // end of else
} // end of public function
else
{
$this->load->view('userLoginPage');
} // end of else
} //end of function
//logout function Start
public function logout()
{
$this->session->sess_destroy();
$this->session->unset_userdata('username','password');
return redirect("userLogin");
}
//logout function End
public function register()
{
$this->load->view('registered');
} //end of register function
public function preRegister()
{
$this->form_validation->set_rules('username', 'Username', 'required|alpha|trim|min_length[5]|max_length[12]|is_unique[user.username]');
$this->form_validation->set_rules('email', 'email','required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('password', 'Password','required');
$this->form_validation->set_rules('confirmpassword', 'ConfirmPassword','required|matches[password]');
$this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");
if($this->form_validation->run())
{
$store = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'dateOfbirth' => $this->input->post('dateOfbirth'),
'password' => $this->input->post('password'),
'confirmpassword'=> $this->input->post('confirmpassword'),
'gender' => $this->input->post('gender')
);
$this->login_model->insert_mod($store);
redirect('UserLogin/index');
} // end of if form_validation
else
{
$this->load->view('registered');
} // end of else
} //end of preRegister function
public function employess() // employes of add ka page view kr rha haaaa
{
$this->load->view('userDashboard');
} // employes of add ka page view kr rha haaaa
public function proEmployess()
{
// $this->load->view('addEmployess');
// $this->load->view('addEmployess');
$employessData = array(
// 'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'address' => $this->input->post('address'),
'department' => $this->input->post('department')
);
$this->login_model->employess_add($employessData);
redirect('UserLogin/employess');
}
public function myProfile(){
// $myProfile = $this->session->userdata();
$this->load->view('headerDashboard.php');
$myProfiledata ['profiles'] = $this->login_model->profileVeiw();
$this->load->view('myProfileView', $myProfiledata);
$this->load->view('footerDashboard.php');
}
// public function myProfile(){
//
// $this->load->view('headerDashboard');
// //$myProfiledata ['datas'] = $this->login_model->veiw_Employess();
// //$employessRecord['datas'] = $this->login_model->veiw_Employess();
// $this->load->view('myProfileView');
// $this->load->view('footerDashboard');
//
// }
}
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model{
public function login_valid($username ,$password)
{
$q = $this->db->where(['username'=>$username, 'password'=>$password])
->get('user');
if( $q->num_rows() )
{
//return pori row ho rhe ha jo database sa arhe haaa
return $q->row();
// return $q->row()->id;
// return TRUE;
}
else
{
return FALSE;
}
}
public function insert_mod($store)
{
$this->db->insert('user', $store);
}
public function employess_add($employessData)
{
$this->db->insert('employessRecord',$employessData);
}
public function view_employess(){
$query = $this->db->get('employessrecord');
return $query;
}public function profileVeiw(){
$queries = $this->db->get('user');
//$queries = $this->session->userdata();
// print_r($queries);
return $queries;
}
}
<div class="col-xl-6">
<div class="card" >
<div class="card-header">
<h4>Profile Details</h4>
</div><br>
<?php foreach($profiles->result() as $profile):?>
<div class="col-md-8">
<label for="name">Username:</label>
<?php echo $profile->username;?>
</div><br><br>
<div class="col-md-8">
<label for="address">Email:</label>
<?php echo $profile->email;?>
</div><br><br>
<div class="col-md-8">
<label for="address">Passowrd:</label>
<?php echo $profile->password;?>
</div><br><br>
<div class="col-md-8">
<label for="address">Gender:</label>
<?php echo $profile->gender;?>
</div><br><br>
<?php endforeach; ?>
</div>
</div>
There is opening tag php tag
so you received the data from form and inserted it in database $this->login_model->insert_mod($store) .
when you call myProfile function you get data in $myProfiledata ['profiles'] = $this->login_model->profileVeiw(); here and passed it to view. if this is correct then first try to print $myProfiledata before passing to the view if everything ok there then try to print $profiles on the "myProfileView" view.
$this->session->userdata('userSessionObj');
here you can check the session data
Edit: Some naming had been mixed up in my attempts to solve it myself. I've fixed the callback etc naming and the same error persists.
I am attempting to create a login page for my codeigniter website. I already have a registration page that correctly inputs usernames and passwords in to a "users" table. I am having issues understanding the syntax of creating the functions needed for custom form validators.
My error is "Unable to access an error message corresponding to your field name" for the password and username custom validators.
Here is the relevant part of the controller "login_ctrl"
class login_ctrl extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login_mdl');
}
function index() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
//Validating Name Field
$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_userCorrect');
//Validating Password Field
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_passwordCorrect');
//variables to pass form input
$username = $this->input->post('username');
$password = $this->input->post('password');
//reload login page if validation fails
if ($this->form_validation->run() == FALSE) {
$this->load->view('login');
} else {
//functions for custom validation
function userCorrect($username) {
$this->load->library('form_validation');
//the loads the model that contains the function to compare input to database data
$userExists = $this->login_mdl->userExists($username);
if ($userExists) {
$this->form_validation->set_message(
'userCorrect', 'correct user.'
);
return true;
} else {
$this->form_validation->set_message(
'userCorrect', 'not a valid user name.'
);
return false;
}
}
function passwordCorrect($password) {
$this->load->library('form_validation');
$passwordExists = $this->login_mdl->passwordCorrect($password);
if ($passwordExists) {
$this->form_validation->set_message('passwordCorrect', 'correct password.');
return true;
} else {
$this->form_validation->set_message('passwordCorrect', 'invalid password.');
return false;
}
}
This is the corresponding view "login"
<?php echo form_open('login_ctrl'); ?>
<h1>Login</h1><hr/>
<?php if (isset($message)) { ?>
<?php } ?>
<?php echo form_label('User Name :'); ?> <?php echo form_error('username'); ?><br />
<?php echo form_input(array('id' => 'username', 'name' => 'username')); ?><br />
<?php echo form_label('Password :'); ?> <?php echo form_error('password'); ?><br />
<?php echo form_input(array('id' => 'password', 'name' => 'password')); ?><br />
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?><br/>
Finally, this is the corresponding model "login_mdl" (I think the issue might be in this guy).
<?php
class login_mdl extends CI_Model{
function __construct() {
parent::__construct();
}
function userExists($username) {
$this->db->select('id');
$this->db->where('username', $username);
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return true;
} else {
return false;
}
}
function passwordCorrect($password) {
$this->db->select('password');
$this->db->where('username', $username);
$query = $this->db->get('users');
$result = $query->row();
return $result->password;
if ('password' == $password) {
return true;
} else {
return false;
}
}
}
?>
I think my issue is related to the db calls and if statements but I've been reading documentation and failing at fixing this for hours so a new pair of eyes would be greatly appreciated.
You need to make your fieldname on your costume rules function is same as your function callback. So, it’s should be like this :
$this->form_validation->set_message(
'userCorrect', 'correct user.'
Do same thing on your other function.
Fetching data in codeigniter getting fatal error for dropdown.
Fatal error: Call to undefined method Blogs_model::where() in C:\xampp\htdocs\project\admin\application\models\blogs_model.php on line 32
Blogs:
function add()
{
$data['categorylist']=$this->blogs_model->categories_dropdown();
$data['mainpage']='blogs';
$data['mode']='add';
$this->load->view('templates/template',$data);
}
function addblogs()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('category_id','Category Name' , 'required');
$this->form_validation->set_rules('blog_title','Blog Title');
$this->form_validation->set_rules('description','Blog Description');
if($this->form_validation->run()== FALSE)
{
$data['categorylist']=$this->blogs_model->categories_dropdown();
$data['mainpage']='blogs';
$data['mode']='add';
$this->load->view('templates/template',$data);
}
else
{
$this -> blogs_model -> insertblogs();
$this->flash->success('<h2>blogs Added Successfully!</h2>');
redirect('blogs');
}
}
Blogs_model
function categories_dropdown()
{
$this->table = 'category';
$this->where('status',1);
$categorylist=$this->dropdown('category_id','category_name');
return $categorylist;
}
View:
<div class="element">
<label for="categoryname"><font color="black">Category Name</font></label>
<?php
$categorylist['']='--Select Category Name--';
$category_id="id='category_id'";
if($this->input->post('category_id')) $selected=$this->input->post('category_id');else $selected='';
echo form_dropdown('category_id',$categorylist,$selected,$category_id);
?>
<?php echo form_error('category_id', '<div class="error">', '</div>'); ?><br/><br/>
</div>
Please update your model as follows.
function categories_dropdown()
{
$this->db->select('category_id','category_name');
$this->db->from('category');
$this->db->where('status',1);
$result = $this->db->get();
return $result->result();
}
How can I retrieve an array in Simple MVC Framework to show error messages if some input fields are not valid?
Controller:
public function index($error)
{
$data ['title'] = $this->language->get('welcome_text');
View::renderTemplate('header', $data);
View::render('insert/form', $data);
View::renderTemplate('footer', $data);
}
public function save()
{
if (isset($_POST)) {
$data ['is_valid'] = \Helpers\Gump::is_valid($_POST, array(
'firstname' => 'required|min_len,2'
));
if ($data ['is_valid'] === true) {
// continue
} else {
$this->index($data ['is_valid']); // show error messages
die();
}
}
}
View:
<div class="alert alert-danger"><?php var_dump($error); ?></div>
... <button type="submit">Save (calls save())</button>
var_dump($error) always shows bool(false).
Basically you wasn't sent error to render, as i see.
public function index($error)
{
$data ['title'] = $this->language->get('welcome_text');
$data ['error'] = $error;
View::renderTemplate('header', $data);
View::render('insert/form', $data);
View::renderTemplate('footer', $data);
}
View
<div class="alert alert-danger"><?php var_dump($data['error']); ?></div>
I'm trying to make alert with cross sign. Unfortunately cross sign not working. I have included jquery before bootstrap.js. Here is my model. Thanks in Advance
public function create(){
$data = array('name'=> $this->input->post('name'));
$this->db->insert('dbname', $data);
echo'
<div class="alert alert-success">
×You have successfully posted
</div>
';
exit;
}
Change this to
×You have successfully posted
this
×You have successfully posted
And as well as use Model to write data into database. Use follows
In controller
public function create(){
$this->load->model('Model_name');
$result = $this->Model_name->insert_data();
if($result == 1)
{
?>
<div class="alert alert-success">
×You have successfully posted
</div>
<?php
}
else
{
?>
<div class="alert alert-error">
×Failed to poste
</div>
<?php
}
}
In Model
public function insert_data()
{
$data = array(
'name'=> $this->input->post('name')
);
if(!$this->db->insert('dbname', $data))
{
return $log = 0;
}
else
{
return $log = 1;
}
}