Message : Undefined Variable userid , workspaceid - php

I am working on a view that show's me the data from the database using joins in textbox.
Now I get the error:
Message: Undefined variable: userid
Message: Undefined variable: workspaceid
Following is my model, View and Controller
Model :-
function getMilestone($taskid, $userid,$workspaceid, $is_master_admin) {
$this->load->database();
if($is_master_admin) {
$this->db->select('user.title as usertitle, workspace.title as workspacetitle,workspace.id as workspaceid,task.title as tasktitle,task.id as taskid, milestone.*');//task.title as tasktitle,task.id as taskid,
} else {
$this->db->select('*');
}
$this->db->from(MILESTONE);
if($is_master_admin) {
$this->db->join(USER, 'milestone.userid = user.id', 'inner');
$this->db->join(WORKSPACE, 'workspace.id = milestone.workspaceid','inner');
$this->db->join(TASK, 'task.id = milestone.taskid', 'inner');
} else {
}
$this->db->where('taskid', $taskid);
if($is_master_admin) {
$this->db->order_by("userid", "asc");
} else {
$this->db->where('userid', $userid);
}
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
Controller :-
function addNew($taskid) {
if ($this->session->userdata('logged_in')) {
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$username = $session_data['username'];
$is_master_admin = $session_data['master'];
$imagethumb = $session_data['imagethumb'];
$pendingbug = $this->bugmodel->getBug($id,$is_master_admin);
$pendingtask = $this->taskmodel->getTask($id,$is_master_admin);
$status_result = $this->getstatus->getEnumValues(MILESTONE,'status');
$result = $this->milestonemodel->getMilestone($taskid, $userid, $workspaceid, $is_master_admin) ;
$data='';
$data = array('username' => $username,
'is_master_admin' => $is_master_admin,
'imagethumb' => $imagethumb,
'result' => $result,
'status_result' => $status_result,
'taskid' => $taskid,
'pendingtask' => $pendingtask,
'pendingbug' => $pendingbug
);
$this->load->view('milestone/add_milestone', $data);
} else {
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
View :-
<div class="control-group">
<label class="control-label">Title<span class="required">*</span></label>
<div class="controls">
<input type="text" name="title" data-required="1" class="span6 m-wrap" readonly="readonly"value="<?php echo $result[0]->taskid; ?>"/> <p><?php echo form_error('title'); ?></p>
<input type="hidden" id="tasktype" name="tasktype" data-required="1" class="span2 m-wrap" value="<?php //echo $task_type ?>"/>
</div>
</div>
<div class="control-group">
<label class="control-label">Workspace</label>
<div class="controls">
<input type="text" readonly="readonly" value="<?php echo $result[0]->workspacetitle; ?>"></input>
</div>
</div>
In my View value is printing using taskid; ?> & workspacetitle; ?> on text box but error is printed on top of the page

Try this controller,
function addNew($taskid) {
if ($this->session->userdata('logged_in')) {
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$username = $session_data['username'];
$is_master_admin = $session_data['master'];
$imagethumb = $session_data['imagethumb'];
$pendingbug = $this->bugmodel->getBug($id, $is_master_admin);
$pendingtask = $this->taskmodel->getTask($id, $is_master_admin);
$status_result = $this->getstatus->getEnumValues(MILESTONE, 'status');
$result = $this->milestonemodel->getMilestone($taskid, $username, $id, $is_master_admin);
$data = '';
$data = array('username' => $username,
'is_master_admin' => $is_master_admin,
'imagethumb' => $imagethumb,
'result' => $result,
'status_result' => $status_result,
'taskid' => $taskid,
'pendingtask' => $pendingtask,
'pendingbug' => $pendingbug
);
$this->load->view('milestone/add_milestone', $data);
} else {
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
I think $userid and $workspace are the $username and $id

There is no values assigned for the variables $userid, $workspaceid
$result = $this->milestonemodel->getMilestone($taskid, $userid, $workspaceid, $is_master_admin) ;

You need to pass the $result into the view. Add the code below :
$data['result']= $result;
before calling :
$this->load->view('milestone/add_milestone', $data);
Then in your view, reference it as :
$result->userid;

Related

Updating value 0 instead of array in the table of multiple checkbox in codeigniter

The form updates the value 0 in the database, it should update the array of check boxes but it's not working.
I've updated the controller and view, please take a look and check if possible.
Controller
function type()
{
if($this->session->userdata("user_id"))//If already logged in
{
$userID = $this->session->userdata("user_id");
$data['user'] = $this->m_user->getRows($userID);
$data['title'] = 'Type | Categories';
$type=$this->form_validation->set_rules('type[]', 'Type', 'required');
if($this->form_validation->run() === FALSE){
$data['categories'] = $this->category_model->get_categories();
$data['main_view']="users/v_type";
$this->load->view('layouts/main',$data);
$this->load->view('layouts/sidebar',$data);
$this->load->view('layouts/footer',$data);
} else {
// Encrypt password
$type = $this->input->post('type');
$this->m_user->type($type);
}else
{
redirect(base_url().'user/');
}
}
View
<form action="<?= base_url()?>user/type" method="post">
<label><input type="checkbox" value="Textile " name="type[]">Textile </label>
<label><input type="checkbox" value="Textile2 " name="type[]">Textile2 </label>
</form>
Model
public function type(){
if($this->session->userdata("user_id"))//If already logged in
{
$userID = $this->session->userdata("user_id");
$data['user'] = $this->m_user->getRows($userID);
$type = $this->input->post('type');
$data=array('type'=>json_encode(implode(",", $type)),);
$this->db->where('user_id', $userID);
return $this->db->update('users', $data);
}
}
What I understand is, issue is in $data parameter
$data['user'] = $this->m_user->getRows($userID); //fetching in parameter data
$type = $this->input->post('type');
$data=array('type'=>json_encode(implode(",", $type)),); //this should have some column like $data['type'] maybe
}
Try this your Updated Model Function code, and tell me what you find
public function type(){
//If already logged in
if($this->session->userdata("user_id"))
{
$userID = $this->session->userdata("user_id");
$data['user'] = $this->m_user->getRows($userID);
$type = $this->input->post('type');
echo'<pre>';print_r($type);
$data=array('type'=>json_encode(implode(",", $type)),);
echo'<pre>';print_r($data);
$this->db->where('user_id', $userID);
$flag = $this->db->update('users', $data);
echo $flag;die;
}
}

how can i print the username of the user who login using session

This is my Users model
public function user_login($email, $password)
{
$this->load->library('session');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data = array(
'id' => $row->id,
'username' => $row->username,
'email' => $row->email
);
$this->session->set_userdata($data);
}
return 1;
} else {
return false;
}
}
On my view i have a div to display the session username of the user
<div>
<?php echo $this->session->userdata('username'); ?>
</div>
It's does not appear What could be wrong?
When I print the contents of userdata() like this
<?php print_r($this->session->userdata()); ?>
I get the response
Array ( [__ci_last_regenerate] => 1502894606 )
simplify your query some
$data = $this->db->where(['email'=>$email, 'password'=>$password])->get('users')->row_array();
if($data){
$_SESSION['id'] = $data['id'];
$_SESSION['username'] = $data['username'];
$_SESSION['email'] = $data['email'];
return TRUE;
}
else{
return FALSE;
}
in view:
<div>
<?php echo $_SESSION['username']; ?>
</div>
try to print it out like this echo $data['username'] where $data is whatever variable you are sending to the view from the controller
If you actually have a username at line with
$row->username
Then right after that array, add
$this->load->vars( array('username' => $row->username) );
And in your view you would be able to get username
echo $username;
This is assuming you are not redirecting after setting your session.

I made a CodeIgniter forgot password function but its not working

Good day! I'm making a forgot password function on the CodeIgniter PHP framework but it doesn't really work. When I click on the send button no email is send.
Some db info:
db name: kadokado
db table: users
db email column: email
db password column: wachtwoord
db id column: user_id
My controller (Auth.php) :
public function forgot()
{
$this->load->model('User_model'); // load user model
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('forgot');
$this->load->view('templates/footer');
}else{
$email = $this->input->post('email');
$clean = $this->security->xss_clean($email);
$userInfo = $this->User_model->getUserInfoByEmail($clean);
if(!$userInfo){
$this->session->set_flashdata('flash_message', 'We hebben dit email adres niet kunnen vinden');
redirect(site_url().'auth/login');
}
if($userInfo->status != true){ //if status is not approved
$this->session->set_flashdata('flash_message', 'Your account is not in approved status');
redirect(site_url().'auth/login');
}
//build token
$token = $this->User_model->insertToken($userInfo->id);
$qstring = $this->base64url_encode($token);
$url = site_url() . 'auth/reset_password/token/' . $qstring;
$link = '' . $url . '';
$message = '';
$message .= '<strong>A password reset has been requested for this email account</strong><br>';
$message .= '<strong>Please click:</strong> ' . $link;
echo $message; //send this through mail
exit;
}
}
public function reset_password()
{
$token = $this->base64url_decode($this->uri->segment(4));
$cleanToken = $this->security->xss_clean($token);
$user_info = $this->User_model->isTokenValid($cleanToken); //either false or array();
if(!$user_info){
$this->session->set_flashdata('flash_message', 'Token is invalid or expired');
redirect(site_url().'auth/login');
}
$data = array(
'voornaam'=> $user_info->voornaam,
'email'=>$user_info->email,
'token'=>base64_encode($token)
);
$this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[wachtwoord]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('reset_password', $data);
$this->load->view('templates/footer');
}else{
$this->load->library('wachtwoord');
$post = $this->input->post(NULL, TRUE);
$cleanPost = $this->security->xss_clean($post);
$hashed = $this->password->create_hash($cleanPost['wachtwoord']);
$cleanPost['wachtwoord'] = $hashed;
$cleanPost['user_id'] = $user_info->id;
unset($cleanPost['passconf']);
if(!$this->User_model->updatePassword($cleanPost)){
$this->session->set_flashdata('flash_message', 'Er is iets foutgegaan');
}else{
$this->session->set_flashdata('flash_message', 'Uw wachtwoord is geupdate, u kunt nu inloggen');
}
redirect(site_url().'auth/login');
}
}
My model (User_model.php) :
<?php
class User_model extends CI_Model {
public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}
public function getUserInfo($user_id)
{
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$user_id.')');
return false;
}
}
public function insertToken($user_id)
{
$token = substr(sha1(rand()), 0, 30);
$date = date('Y-m-d');
$string = array(
'token'=> $token,
'user_id'=>$user_id,
'created'=>$date
);
$query = $this->db->insert_string('tokens',$string);
$this->db->query($query);
return $token . $user_id;
}
public function isTokenValid($token)
{
$tkn = substr($token,0,30);
$uid = substr($token,30);
$q = $this->db->get_where('tokens', array(
'tokens.token' => $tkn,
'tokens.user_id' => $uid), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
$created = $row->created;
$createdTS = strtotime($created);
$today = date('Y-m-d');
$todayTS = strtotime($today);
if($createdTS != $todayTS){
return false;
}
$user_info = $this->getUserInfo($row->user_id);
return $user_info;
}else{
return false;
}
}
}
?>
My view (reset_password.php) :
<?php include_once ('templates/header.php'); ?>
<?php include_once ('templates/sidebar2.php'); ?>
<div class="col-lg-4 col-lg-offset-4">
<h2>Reset your password</h2>
<h5>Hello <span><?php echo $voornaam; ?></span>, Voer uw wachtwoord 2x in aub</h5>
<?php
$fattr = array('class' => 'form-signin');
echo form_open(site_url().'auth/reset_password/token/'.$token, $fattr); ?>
<div class="form-group">
<?php echo form_password(array('name'=>'wachtwoord', 'id'=> 'wachtwoord', 'placeholder'=>'Wachtwoord', 'class'=>'form-control', 'value' => set_value('wachtwoord'))); ?>
<?php echo form_error('password') ?>
</div>
<div class="form-group">
<?php echo form_password(array('name'=>'passconf', 'id'=> 'passconf', 'placeholder'=>'Confirm Password', 'class'=>'form-control', 'value'=> set_value('passconf'))); ?>
<?php echo form_error('passconf') ?>
</div>
<?php echo form_hidden('user_id', $user_id);?>
<?php echo form_submit(array('value'=>'Reset Password', 'class'=>'btn btn-lg btn-primary btn-block')); ?>
<?php echo form_close(); ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
I hope someone can help me out!

CodeIgniter login using passwordHash and passwordSalt

My question..The variable $salted returns a random UUID then md5's it.What I need for the login to work though is for the variable $salted to return the value of passwordSalt field in my database from the auth table that corresponds to t1.PrincipalID = t2.UUID .How do I give the variable $salted this value?
Basically how do i give a variable a value from my database without user input.Thank you.
code snippets..the essential part of my code.
my view...
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<form>
<input type="text" name="FirstName" placeholder="firstName">
<input type="password" name="passwordHash" placeholder="Password">
<input type="submit" name="login" class="login login-submit" value="Login">
</form></div>
my controller...
Function check_database($password)
{
//Field validation succeeded. Validate against database
$firstName = $this->input->post('FirstName');
$password = $this->input->post('passwordHash');
//query the database
$result = $this->users_model->login($firstName,$password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'PrincipalID' => $row->PrincipalID,
'FirstName' => $row->FirstName
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
my model..
function login($firstName,$password)
{
$salted = sprintf('%s', md5(_uuid()));
$hash = md5(md5($password) . ":" . $salted);
$this->db->select('PrincipalID, FirstName')
->from('useraccounts AS t1, auth AS t2')
->where('t1.PrincipalID = t2.UUID')
->where('t1.FirstName', $firstName)
->where('t2.passwordHash', $hash);
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}

Use CodeIgniter (MVC) to Connect Form to Database

My assignment is to use CodeIgniter (an MVC framework) and connect the form that's in the view to the database. I have done most of the code, and I believe most of it is correct. I finally got the view page (form) to load, but I cannot get the Register function in the controller to load. Does anyone see any errors in my code? It would help me a great deal.
form.php (view):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php //echo $title;?></title>
</head>
<body>
<h1>Register for a Dorm</h1>
<form method="post"
action="http://ispace.ci.fsu.edu/~trm07/assignment4/index.php/controller/register/">
<fieldset>
<label>First Name</label>
<input type="text" name="first_name"/>
<label>Last Name</label>
<input type="text" name="last_name"/>
</fieldset>
<label>Undergraduate Level:</label>
<?php
//dropdown list for level
echo '<select name="level">';
$level = array('Freshman','Sophomore', 'Junior', 'Senior');
foreach ($level as $selection) {
echo '<option value="'.$selection.'">'.$selection.'</option>';
}
echo '</select>';
?>
<label>Gender:</label>
<?php
//dropdown list for gender
echo '<select name="gender">';
$gender = array('Male','Female');
foreach ($gender as $gend_selection) {
echo '<option
value="'.$gend_selection.'">'.$gend_selection.'</option>';
}
echo '</select>';
//radio buttons for dorms
$requested_dorm = array('Landis','Salley','DeGraff','Cawthon','Reynolds');
echo("<fieldset><legend>Requested Dorm</legend>");
foreach ($requested_dorm as $dorm_names){
echo "<input type='radio' name='dorm_name' value='$dorm_names' />
$dorm_names <br />";
}
?>
<br><input type="submit" value="Register">
</body>
</html>
controller.php
<?php
class Controller extends CI_Controller {
function index()
{ //$data['title'] = "Register for a Dorm";
$this->load->view('form');
}
function show()
{
$this->load->model('model');
$dorms = $this->model->get_dorms();
foreach($dorms as $dorm){
if($dorm['dorm_name'] == $dorm_name)
$chosen_dorm = $dorm['dorm_name'];
}
}
function register()
{
$this->load->library('form_validation');
$view_data = array('message' => '');
//If the form was submitted, process it
if (count($_POST) > 0)
{ $dorm_name = $this->input->post('dorm_name');
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$level = $this->input->post('level');
$gender = $this->input->post('gender');
{
//Validate the input
$this->form_validation->set_rules('first_name', 'First Name',
'required|strip_tags|trim');
$this->form_validation->set_rules('last_name', 'Last Name',
'required|strip_tags|trim');
$val_result = $this->form_validation->run();
//Add the data to the database
if ($val_result == TRUE)
{
$this->load->model('model');
$db_result = $this->model->add_student_to_dorm($_POST['dorm_name'],
$_POST['first_name'], $_POST['last_name'], $_POST['level'], $_POST['gender']);
if ($db_result == TRUE)
{
$view_data['message'] = "Added student to the dorm!";
//$view_data['message'] = "Added" . "$_POST['first_name']" . " " .
"$_POST['last_name']". " to " . "$_POST['dorm_name']" . " hall.";
}
else
{
$view_data['message'] = "An error occured adding the student to the dorm!";
}
}
}
$this->load->view('form',$view_data);
//$this->load->model('model');
//$this->model->get_students();
}
}
}
?>
model.php:
<?php
class Model extends CI_Model {
//function to take student info posted from form, and adds to database
public function add_student_to_dorm()
{
$this->load->database($dorm_name, $first_name, $last_name, $level, $gender);
$data = array
(
'dorm_name' =>$dorm_name,
'student_fname' => $first_name,
'student_lname' => $last_name,
'student_level' => $level,
'student_gender' => $gender,
);
$result = $this->db->insert('student', $data);
return $result;
}
//get dorm table results in an array from database, return its rows
public function get_dorms()
{
$this->load->database();
$dorms = $this->db->get('dorm');
$rows = $dorms->result_array();
return $rows;
}
//get student table results in an array from database, return its rows
public function get_students()
{
$this->load->database();
$students = $this->db->get('student');
$stu_rows = $students->result_array();
return $stu_rows;
}
}
?>
Here you called add_student_to_dorm() function in the controller page with parameters but in model you doesnot take that parameters that is
$db_result = $this->newmodel->add_student_to_dorm($_POST['dorm_name'],
$_POST['first_name'], $_POST['last_name'], $_POST['level'], $_POST['gender']);
this is the function in controller but your model is only have
public function add_student_to_dorm() no variables So change
public function add_student_to_dorm() to
public function add_student_to_dorm($dorm_name, $first_name, $last_name, $level, $gender)
and also change
$this->load->database($dorm_name, $first_name, $last_name, $level, $gender); to
$this->load->database(); in model then it is working

Categories